IO流-数据流、对象流(序列化与反序列化)、打印流

数据流:DataOutputStream

 
  
  1. public class TestDataStream 
  2.     public void writeData() 
  3.     { 
  4.         double[] arrays = new double[1000]; 
  5.         Arrays.fill(arrays, Math.PI); 
  6.          
  7.         String fileName = "F:/java/test2.txt"
  8.         FileOutputStream out; 
  9.         DataOutputStream dos = null
  10.         try 
  11.         { 
  12.             out = new FileOutputStream(fileName); 
  13.             dos = new DataOutputStream(out); 
  14.             for (int i = 0; i < arrays.length; i++) 
  15.             { 
  16.                 dos.writeDouble(arrays[i]); 
  17.             } 
  18. //            dos.write(123); 
  19. //            dos.writeBoolean(true); 
  20. //            dos.writeChar('Z'); 
  21. //            dos.writeDouble(Math.PI); 
  22.             dos.flush(); 
  23.         } 
  24.         catch (FileNotFoundException e) 
  25.         { 
  26.             e.printStackTrace(); 
  27.         } 
  28.         catch (IOException e) 
  29.         { 
  30.             e.printStackTrace(); 
  31.         } 
  32.         finally 
  33.         { 
  34.             if (dos != null
  35.             { 
  36.                 try 
  37.                 { 
  38.                     dos.close(); 
  39.                 } 
  40.                 catch (IOException e) 
  41.                 { 
  42.                     e.printStackTrace(); 
  43.                 } 
  44.             } 
  45.         } 
  46.     } 
  47.      
  48.     public static void main(String[] args) 
  49.     { 
  50.         TestDataStream tds = new TestDataStream(); 
  51.         tds.writeData();      
  52.     } 
  53.      

 

数据流:DataInputStream

 
  
  1. public class TestDataInputStream 
  2.     public static void main(String[] args) 
  3.     { 
  4.         String fileName = "F:/java/test2.txt"
  5.         FileInputStream in = null
  6.         DataInputStream dis = null
  7.         try 
  8.         { 
  9.             in = new FileInputStream(fileName); 
  10.             dis = new DataInputStream(in); 
  11.             for (int i = 0; i < 1000; i++) 
  12.             { 
  13.                 System.out.println(dis.readDouble() + "i: " + i); 
  14.             } 
  15. //            System.out.println(dis.read()); 
  16. //            System.out.println(dis.readBoolean()); 
  17. //            System.out.println(dis.readChar()); 
  18. //            System.out.println(dis.readDouble()); 
  19.         } 
  20.         catch (FileNotFoundException e) 
  21.         { 
  22.             e.printStackTrace(); 
  23.         } 
  24.         catch (IOException e) 
  25.         { 
  26.             e.printStackTrace(); 
  27.         } 
  28.         finally 
  29.         { 
  30.             if (dis != null
  31.             { 
  32.                 try 
  33.                 { 
  34.                     dis.close(); 
  35.                 } 
  36.                 catch (IOException e) 
  37.                 { 
  38.                     e.printStackTrace(); 
  39.                 } 
  40.             } 
  41.         } 
  42.     } 

 

对象流:ObjectOutputStream(unserializable)、ObjectInputStream(serializable)

 
  
  1. public class TestSerializable 
  2.     public static void main(String[] args) 
  3.     { 
  4.         TestSerializable ts = new TestSerializable(); 
  5.         ts.serializable(); 
  6.         ts.unserializable(); 
  7.     } 
  8.     public void serializable()//序列化,写入 
  9.     { 
  10.         String filename = "F:/java/stu.txt"
  11.         Student s1 = new Student("haoyouduo",1987); 
  12.          
  13.         FileOutputStream fis =null
  14.         ObjectOutputStream ois = null
  15.         try 
  16.         { 
  17.             fis = new FileOutputStream(filename); 
  18.             ois = new ObjectOutputStream(fis); 
  19.             ois.writeObject(s1); 
  20.             ois.flush(); 
  21.         } 
  22.         catch (FileNotFoundException e) 
  23.         { 
  24.             e.printStackTrace(); 
  25.         } 
  26.         catch (IOException e) 
  27.         { 
  28.             e.printStackTrace(); 
  29.         } 
  30.         finally 
  31.         { 
  32.             if(null != ois) 
  33.             { 
  34.                 try 
  35.                 { 
  36.                     ois.close(); 
  37.                 } 
  38.                 catch (IOException e) 
  39.                 { 
  40.                     e.printStackTrace(); 
  41.                 } 
  42.             } 
  43.         } 
  44.          
  45.     } 
  46.     public void unserializable()//反序列化,读取 
  47.     { 
  48.         String filename = "F:/java/stu.txt"
  49.         FileInputStream fis = null
  50.         ObjectInputStream ois = null
  51.         try 
  52.         { 
  53.             fis = new FileInputStream(filename); 
  54.             ois = new ObjectInputStream(fis); 
  55.             Student s = (Student)ois.readObject(); 
  56.             System.out.println(s); 
  57.              
  58.         } 
  59.         catch (FileNotFoundException e) 
  60.         { 
  61.             e.printStackTrace(); 
  62.         } 
  63.         catch (IOException e) 
  64.         { 
  65.             e.printStackTrace(); 
  66.         } 
  67.         catch (ClassNotFoundException e) 
  68.         { 
  69.             e.printStackTrace(); 
  70.         } 
  71.         finally 
  72.         { 
  73.             if(ois != null
  74.             { 
  75.                 try 
  76.                 { 
  77.                     ois.close(); 
  78.                 } 
  79.                 catch (IOException e) 
  80.                 { 
  81.                     e.printStackTrace(); 
  82.                 } 
  83.             } 
  84.         } 
  85.          
  86.     } 
  87.  
  88. class Student implements Serializable//对象类必须实现可序列化的 
  89.     String name; 
  90.     int age; 
  91.     public Student(String name,int age) 
  92.     { 
  93.         this.name = name; 
  94.         this.age = age; 
  95.     } 
  96.     @Override 
  97.     public String toString() 
  98.     { 
  99.         return "Student [name=" + name + ", age=" + age + "]"
  100.     } 
  101.  
  102.      

 

打印流:PrintStream

 
  
  1. public class Test 
  2.     public static void main(String[] args) 
  3.     { 
  4.         boolean flag = 2 > 1
  5.         if (flag) 
  6.         { 
  7.             System.out.println("sss"); 
  8.         } 
  9.         else 
  10.         { 
  11.             System.out.println("aaa"); 
  12.         } 
  13.          
  14.         System.out.println("使用printStream之前"); 
  15.  
  16.         /** 
  17.          * 上面部分的内容将打印在控制台里 
  18.          * 下面部分的内容不会打印在控制台里,而是文件里 
  19.          */ 
  20.          
  21.         String filename = "f:/java/log.txt"
  22.         FileOutputStream fos; 
  23.         PrintStream ps = null
  24.         try 
  25.         { 
  26.             fos = new FileOutputStream(filename); 
  27.             ps = new PrintStream(fos); 
  28.             System.setOut(ps); 
  29.             System.out.println("这将打印在文件里"); 
  30.             System.out.println("使用printStream之后"); 
  31.         } 
  32.         catch (FileNotFoundException e) 
  33.         { 
  34.             e.printStackTrace(); 
  35.         } 
  36.         finally 
  37.         { 
  38.             if (ps != null
  39.             { 
  40.                 ps.close(); 
  41.             } 
  42.         } 
  43.     } 

 





本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1191874,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值