Latest web development tutorials

Java DataOutputStream class

A data output stream lets an application machine-independent manner Java basic data type is written to the underlying output stream.

The following constructor is used to create a data output stream object.

DataOutputStream out = DataOutputStream(OutputStream  out);

After you create an object, you can reference the following list gives the method of convective write or other operations.

No. Method Description
1 public final void write (byte [] w, int off, int len) throws IOException
The specified byte array starting at offset off beginning len bytes written to this byte array output stream.
2 Public final int write (byte [] b) throws IOException
The specified byte to this byte array output stream.
3
  1. public final void writeBooolean () 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 are specified in the basic data types bytes to be written 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 a sequence of bytes in the string to the underlying output stream, each character in the string is written sequentially, and discarding its high eight.

Examples

The following example illustrates the DataInputStream and DataOutputStream use this example to read from a text file test.txt 5 lines in and converted to uppercase letters, the last saved in another file test1.txt in.

import java.io.*;

public class Test{
   public static void main(String args[])throws IOException{

      DataInputStream d = new DataInputStream(new
                               FileInputStream("test.txt"));

      DataOutputStream out = new DataOutputStream(new
                               FileOutputStream("test1.txt"));

      String count;
      while((count = d.readLine()) != null){
          String u = count.toUpperCase();
          System.out.println(u);
          out.writeBytes(u + "  ,");
      }
      d.close();
      out.close();
   }
}

The above examples compiled results are as follows:

THIS IS TEST 1  ,
THIS IS TEST 2  ,
THIS IS TEST 3  ,
THIS IS TEST 4  ,
THIS IS TEST 5  ,