黑马程序员系列第七篇 IO(1)

 

(前言:本篇文章主要依据毕向东老师的课程视频整理而成,如要详细学习,请观看毕老师视频  百度网盘链接地址:http://pan.baidu.com/s/1mgrkJWW)

目录:1、字节流 基类InputStream\OutputStream    2、字符流 基类Reader\Writer   

      注意:由这四类派生出来的子类名称都是以其父类作为子类名的后缀。

 1、字节流 基类InputStream\OutputStream  

 

下图为字节流类体系结构图,红色部分为常用部分

 

 

举几个典型例子来更好的了解如何应用

三种以字节流形式读取指定文件输出到控制台的方法(为简便起见,都被封装于各个函数中)

 1 //读取字节流,单个字符为单位进行
 2 public static void readStream(String filename){
 3     
 4       FileInputStream fis=null;
 5     try {
 6         fis=new FileInputStream(filename);
 7         //以read方法的还回值为条件,判断是否读取结束,遇到非char型的就鸡鸡了
 8         int cha;
 9         while((cha=fis.read())!=-1){
10             System.out.println((char)cha);
11         }
12         System.out.println("逐字符方式读取文件内容已结束...");
13     } catch (FileNotFoundException e) {
14         e.printStackTrace();
15     } catch (IOException e) {
16         e.printStackTrace();
17     }finally{try {
18         if(fis!=null) 
19             fis.close();//读取结束,判断若流已被创建,则关闭流资源
20     } catch (IOException e) {
21         e.printStackTrace();
22     }
23     }
24 }
25 //读取字节流,字符数组为单位进行,比较好用
26 public static void readStream1(String filename){
27     
28         FileInputStream fis=null;
29     try {
30         fis=new FileInputStream(filename);
31         //定义每次读取的字符数组大小
32         byte[] cha=new byte[1024];
33         int len=0;
34         while((len=fis.read(cha))!=-1){
35             System.out.println(new String(cha,0,len));
36         }
37         System.out.println("逐字符数组读取结束...");
38     } catch (FileNotFoundException e) {
39         e.printStackTrace();
40     } catch (IOException e) {
41         e.printStackTrace();
42     }finally{try {
43         if(fis!=null) 
44             fis.close();//读取结束,判断若流已被创建,则关闭流资源
45     } catch (IOException e) {
46         e.printStackTrace();
47     }
48     }
49 }
50 
51   //慎用,读取文件太大会出问题。
52 public static void readStream2(String filename){
53     
54       FileInputStream fis=null;
55     try {
56         fis=new FileInputStream(filename);
57         //定义每次读取的字符数组的大小为整个文件全部
58         byte[] cha=new byte[fis.available()];
59         fis.read(cha);
60         System.out.println(new String(cha));
61         System.out.println("整篇一次性读取结束...");
62         
63     } catch (FileNotFoundException e) {
64         e.printStackTrace();
65     } catch (IOException e) {
66         e.printStackTrace();
67     }finally{try {
68         if(fis!=null)
69             fis.close();
70     } catch (IOException e) {
71         e.printStackTrace();
72     }
73     }    
74 }

 

 2、字符流 基类Reader\Writer  

   下图为字节流类体系结构图,红色部分为常用部分

 

三种以字符流方式读取数据的办法

 1 //逐字符的原始读取文件中字符,读取的结果打印到控制台    
 2 public static void readChar(String filename){
 3     
 4     FileReader fr=null;
 5     try {
 6         fr=new FileReader(filename);
 7         //以read函数返回结果来判断是否读完,进行循环
 8         int cha;
 9         while((cha=fr.read())!=-1){
10             System.out.print((char)cha);
11         }
12     } catch (FileNotFoundException e) {
13         e.printStackTrace();
14     } catch (IOException e) {
15         e.printStackTrace();
16     }finally{
17         try {
18             if(fr!=null)  
19                 fr.close();//先判断流资源是否存在,然后关闭流资源
20         } catch (IOException e) {
21             e.printStackTrace();
22         }
23     }
24 }
25 //以字符串数组为单位读取数据,结果打印到控制台
26 public static void readChars(String filename){
27     
28     FileReader fr=null;
29     try {
30         fr=new FileReader(filename);
31         //定义字符数组的大小
32         char[] chars=new char[1024];
33         int number;      
34         while((number=fr.read(chars))!=-1){
35             //将字符数组转换成字符串,打印结果
36             System.out.println(new String(chars,0,number));
37         }         
38     } catch (FileNotFoundException e) {
39         e.printStackTrace();
40     } catch (IOException e) {
41         e.printStackTrace();
42     }finally{
43         try {
44             if(fr!=null) 
45                 fr.close();//先判断流资源是否存在,然后关闭流资源
46         } catch (IOException e) {
47             e.printStackTrace();
48         }
49     }
50 }
51 //字符操作,以字符串数组为单位读取数据,结果打印到控制台并且将行数打印出。增加了一个缓冲区,提高读取效率。
52 public static void bufferReader(String filename){
53     
54      LineNumberReader buff=null;
55     try {
56         buff=new LineNumberReader(new  FileReader(filename));        
57         buff.setLineNumber(0);//指定行号
58         
59         String line;
60         while((line=buff.readLine())!=null){
61             System.out.println(buff.getLineNumber()+":"+line);//打印带行号的内容
62         }
63     } catch (FileNotFoundException e) {
64         e.printStackTrace();
65     } catch (IOException e) {
66         e.printStackTrace();
67     }finally{
68         try {
69           if(buff!=null)
70             buff.close();//先判断流资源是否存在,然后关闭流资源
71         } catch (IOException e) {
72             // FIXME Auto-generated catch block
73             e.printStackTrace();
74         }
75     }
76 }

 

 两种方式从控制台读取字符存入自定文件中

 1 //字符操作,读取控制台录入,读到kill行结束操作,保存到filename.txt文件中,以行为单位操作    
 2     public static void write(String filename){
 3             //读取控制台录入   
 4             System.out.println("请录入(kill为结束命令,文件保存位置-"+filename+"):");
 5              BufferedReader buff=null;        
 6             FileWriter fw=null;
 7             try {    
 8                 buff = new BufferedReader(new InputStreamReader(System.in));
 9                 fw = new FileWriter(filename,true);
10                 //只要读的行数据不为空就循环向文件中写数据
11                 String line;
12                 while((line=buff.readLine())!=null){
13                     if(line.equals("kill")) 
14                         break; 
15                         fw.write(line);                        
16                 }
17                 System.out.println("录入已经结束...");//录入结束提示
18             } catch (IOException e) {
19                 e.printStackTrace();
20             }finally{
21                 try {
22                     if(fw!=null)  
23                         fw.close();//关闭流资源
24                 } catch (IOException e) {
25                     e.printStackTrace();
26                 }
27                 try {
28                     if(buff!=null)  
29                         buff.close();//关闭流资源
30                 } catch (IOException e) {
31                     e.printStackTrace();
32                 }    
33         }
34     }
35     //字符操作,读取控制台录入,保存到filename.txt文件中,加了一个缓冲区,提高存储效率
36     public static void bufferWriter(String filename){
37         
38         BufferedWriter buffwr=null;
39         BufferedReader buffre=null;
40         try {
41               FileWriter fw=new FileWriter(filename,true);
42               buffre=new BufferedReader(new InputStreamReader(System.in)); 
43               buffwr=new BufferedWriter(fw);
44               System.out.println("请录入(kill为结束命令,文件保存位置-"+filename+"):");
45               
46               String line;
47               while((line=buffre.readLine())!=null){
48                   if(line.equals("kill")) 
49                       break;
50                   buffwr.write(line);
51                   buffwr.newLine();//将换行符读进保存
52               }
53              System.out.println("录入已经结束...");
54               
55         } catch (IOException e) {
56             e.printStackTrace();
57         }finally{    
58                 try {
59                     if(buffwr!=null)
60                     buffwr.close();
61                 } catch (IOException e) {
62                     e.printStackTrace();
63                 }
64                 try {
65                     if(buffre!=null)
66                     buffre.close();
67                 } catch (IOException e) {
68                     e.printStackTrace();
69                 }
70         }
71     }    

 

两种文本文件拷贝方式

 1 //复制文本文件,从from到to
 2 public static void copyTxt(String from,String to){
 3        //定义读写文件    
 4     FileReader fr=null;
 5     FileWriter fw=null;
 6         
 7     try {
 8         //创建读写文件对象
 9         fr=new FileReader(from);
10         fw=new FileWriter(to,true);
11         //定义数组大小
12         char[] buff=new char[1024];
13         int num;
14         while((num=fr.read(buff))!=-1){
15             fw.write(buff, 0, num);
16         }
17         System.out.println("文件已经从   "+from+"  文件夹复制到了   "+to+"  文件夹...");
18     } catch (IOException e) {
19         e.printStackTrace();
20     }finally{
21         try {
22             if(fr!=null) 
23                fr.close();
24         }
25         catch (IOException e) {
26             e.printStackTrace();
27         }
28         try{
29             if(fw!=null)  
30                 fw.close();
31         }
32         catch (IOException e) {
33             e.printStackTrace();
34         }
35     }
36 }
37 //用装饰类保证一下,提高效率
38 public static void bufferCopyTxt(String from,String to){
39     
40         BufferedReader br=null;
41         BufferedWriter bw=null;        
42     try {        
43         br=new BufferedReader(new FileReader(from));
44         bw=new BufferedWriter(new FileWriter(to));
45         
46         String line;
47         while((line=br.readLine())!=null){
48             bw.write(line);
49             bw.flush();
50         }
51         System.out.println("文件已经从   "+from+"  文件夹复制到了   "+to+"  文件夹...");        
52     } catch (IOException e) {
53         e.printStackTrace();
54     }finally{        
55             try {
56                 if(br!=null)
57                 br.close();
58             } catch (IOException e) {
59                 // FIXME Auto-generated catch block
60                 e.printStackTrace();
61             }
62             try {
63                 if(bw!=null)
64                 br.close();
65             } catch (IOException e) {
66                 // FIXME Auto-generated catch block
67                 e.printStackTrace();
68             }
69     }
70 }

 

复制图片的操作,图片必须以字节流的方式操作

 1 //复制图片,从from文件到to文件。用到字节流
 2 public static void copyImag(String from,String to){
 3     //定义输入输出字节流
 4     FileInputStream fis=null;
 5     FileOutputStream fos=null;
 6     
 7     try {
 8         fis=new FileInputStream(from);
 9         fos=new FileOutputStream(to);
10         //定义每次复制的单位字符数组大小
11         byte[] cha=new byte[1024];
12         int len=0;
13         while((len=fis.read(cha))!=-1){
14             fos.write(cha,0,len);
15         }
16     } catch (FileNotFoundException e) {
17         e.printStackTrace();
18     } catch (IOException e) {
19         e.printStackTrace();
20     }finally{
21         try {if(fis!=null)
22                fis.close();
23         } catch (IOException e) {
24             // FIXME Auto-generated catch block
25             e.printStackTrace();
26         }
27         try {if(fis!=null)
28                fis.close();
29         } catch (IOException e) {
30             // FIXME Auto-generated catch block
31             e.printStackTrace();
32         }
33     }
34 }

 

 

 

       初学者难免错误,欢迎评判指教,持续更正ing...........

 

posted @ 2015-11-17 22:16  YerJustDoIt  阅读(323)  评论(0编辑  收藏  举报