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