// 使用 DataOutputStream 进行写操作
FileOutputStream fos = new FileOutputStream("c:/myDoc/hello.txt");
// 实例化对象
BufferedOutputStream bos = new BufferedOutputStream();
DataOutputStream dos = new DataOutputStream();
dos.writeByte(1);
dos.writeLong(2);
dos.writeChar('c');
dos.writeUTF("hello");
// 关闭流
dos.close();
bos.close();
fos.close();
System.out.println("====完成文件写入=====");
System.out.println("开始读取文件");
FileInputStream fis = new FileInputStream("c:/myDoc/hello.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
System.out.println(dis.readByte());
System.out.println(dis.readLong());
System.out.println(dis.readChar());
System.out.println(dis.readUTF());
// 关闭流
dis.close();
bis.close();
fis.close();
System.out.println("====文件读取完毕=====");
// DataOutputStream:写入流,读取流,一定注意写的顺序和读的顺序一定要保持一致,不然会抛出可怕的异常;
|