DataOutputStream and DataInputStream: read and write by data types : DataInputStream « File Input Output « Java






DataOutputStream and DataInputStream: read and write by data types

 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Main {
  public static void main(String[] atgs) throws Exception {
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("java.txt")));
    out.writeUTF("");
    out.writeBytes("a");
    out.writeChars("aaa");
    out.close();
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("java.txt")));
    System.out.println(in.readUTF());
    byte[] buf = new byte[1024];
    int len = in.read(buf);
    System.out.println(new String(buf, 0, len));
    in.close();
  }
}

   
  








Related examples in the same category

1.new DataInputStream(new BufferedInputStream(new FileInputStream()))
2.Create DataInputStream from FileInputStream and read different types of data
3.Read short type of data with DataInputStream
4.Read different data types from DataInputStream
5.Read boolean from file using DataInputStream
6.Read byte array from file using DataInputStream
7.Read byte from file using DataInputStream
8.Read char from file using DataInputStream
9.Read double from file using DataInputStream
10.Read float from file using DataInputStream
11.Read int from file using DataInputStream
12.Read long from file using DataInputStream
13.Read short from file using DataInputStream
14.Read unsigned byte from file using DataInputStream
15.Use DataInputStream to create double
16.Check the class version