6、数据流
1 package Io; 2 3 import java.io.DataInputStream; 4 import java.io.DataOutputStream; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.FileOutputStream; 8 import java.io.IOException; 9 10 public class TestWriteNumber_Io { 11 public static void main(String[] args) { 12 FileOutputStream fos = null; 13 DataOutputStream dos = null; 14 FileInputStream fis = null; 15 DataInputStream dis = null; 16 17 try { 18 fos = new FileOutputStream("C:\\Users\\Samuel\\Pictures\\abc.dat"); 19 // 如果希望存储基本数据类型就用DataOutputStream,也是过滤流 20 dos = new DataOutputStream(fos); 21 dos.writeInt(11231231); 22 dos.writeInt(2); 23 dos.writeInt(3); 24 dos.writeInt(4); 25 fis = new FileInputStream("C:\\Users\\Samuel\\Pictures\\abc.dat"); 26 // 从文件读取数据用DataInputStream,同样是过滤流 27 dis = new DataInputStream(fis); 28 // dis = new DataInputStream(new FileInputStream("C:\\Users\\Samuel\\Pictures\\abc.dat")); 29 System.out.println(dis.readInt()); 30 System.out.println(dis.readInt()); 31 System.out.println(dis.readInt()); 32 System.out.println(dis.readInt()); 33 } catch (FileNotFoundException e) { 34 e.printStackTrace(); 35 } catch (IOException e) { 36 e.printStackTrace(); 37 } finally { 38 if (dos != null) 39 try { 40 dos.close(); 41 } catch (IOException e) { 42 // TODO Auto-generated catch block 43 e.printStackTrace(); 44 } 45 } 46 } 47 }