YouTip LogoYouTip

Java Dataoutputstream

# Java DataOutputStream Class [![Image 3: Java Stream](#) Java Stream](#) * * * The data output stream allows an application to write Java primitive data types to an output stream in a machine-independent way. The following constructor is used to create a data output stream object. ```java DataOutputStream out = new DataOutputStream(OutputStream out); Once the object is successfully created, you can refer to the methods listed below to perform write operations or other operations on the stream. | No. | Method Description | | --- | --- | | 1 | **public final void write(byte[] w, int off, int len) throws IOException** Writes `len` bytes from the specified byte array starting at offset `off` to this byte array output stream. | | 2 | **public final int write(byte[] b) throws IOException** Writes the specified byte to this byte array output stream. | | 3 | 1. **public final void writeBoolean() throws IOException,** 2. **public final void writeByte() throws IOException,** 3. **public final void writeShort() throws IOException,** 4. **public final void writeInt() throws IOException** These methods write the specified primitive data type as bytes to the output stream. | | 4 | **public void flush() throws IOException** Flushes this output stream and forces any buffered output bytes to be written out. | | 5 | **public final void writeBytes(String s) throws IOException** Writes the string as a byte sequence to the underlying output stream. Each character in the string is written in sequence, and its high eight bits are discarded. | ### Example The following example demonstrates the use of DataInputStream and DataOutputStream. The example reads 5 lines from a text file `test.txt`, converts them to uppercase, and saves them in another file `test1.txt`. The content of `test.txt` is as follows: tutorial1 tutorial2 tutorial3 tutorial4 tutorial5 ## Example ```java import java.io.*; public class Test { public static void main(String args[]) throws IOException { DataInputStream in = new DataInputStream(new FileInputStream("test.txt")); DataOutputStream out = new DataOutputStream(new FileOutputStream("test1.txt")); BufferedReader d = new BufferedReader(new InputStreamReader(in)); String count; while ((count = d.readLine()) != null) { String u = count.toUpperCase(); System.out.println(u); out.writeBytes(u + " ,"); } d.close(); out.close(); } } The compilation and execution results of the above example are as follows: TUTORIAL1 , TUTORIAL2 , TUTORIAL3 , TUTORIAL4 , TUTORIAL5 , * * Java Stream](#)
← Java FileJava Bytearrayoutputstream β†’