import java.io.*;
public class DataStreamTest {
public static void main(String [] args) {
try {
FileOutputStream fos = new FileOutputStream("count.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF("ab中国"); // 会写入额外的信息
dos.writeBytes("ab中国"); // 不容易读取
dos.writeChars("ab中国"); // 不容易读取
dos.close();
FileInputStream fis = new FileInputStream("count.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
System.out.println(dis.readUTF()); // 可以读出来
byte[] buf = new byte[1024];
int len = dis.read(buf); // 读不出来,不知何时停止
System.out.println(new String(buf, 0, len));
fis.close();
} catch (Exception e ) {
e.printStackTrace();
}
}
}
关闭上层流类时,底层被包装流类也自动关闭