java IO 流的学习

本文转自http://www.cnblogs.com/killbug/archive/2012/08/15/2640845.html

IO入门代码阅读:

字节流:

 1 private void writeTxt(String path, String value) throws IOException{
 2         OutputStream fos = new FileOutputStream(path);//构造方法1
 3         fos.write(value.getBytes());
 4         fos.close();
 5     }
 6     private void readTxt(String path) throws IOException{
 7         File file = new File(path);
 8         InputStream fis = new FileInputStream(path);//构造方法2
 9         byte b[] = new byte[(int)file.length()] ;
10         fis.read(b);
11         System.out.print(new String(b));
12         fis.close();
13     }

字符流:

 1 private void writeTxt(String path, String value) throws IOException{
 2         Writer writer = new FileWriter(path);
 3         writer.write(value);
 4         writer.close();
 5     }
 6     
 7     private void readTxt(String path) throws IOException{
 8         Reader reader = new FileReader(path);
 9         char c[] = new char[1024] ;
10         int len = reader.read(c);
11         System.out.print(new String(c, 0, len));
12         reader.close();
13     }

 

 

 

 

posted @ 2012-10-24 16:03  寒剑飘香  阅读(93)  评论(0编辑  收藏  举报