黑马程序员技术交流社区

标题: 与文件相关的读写,实现了文件复制 [打印本页]

作者: 唯爱唐嫣    时间: 2016-3-30 23:45
标题: 与文件相关的读写,实现了文件复制
与IO相关的东西实在是很繁杂,这里分享一个例子供欣赏

1 import java.io.*;
2 public class TextRead{
3
4 public static void main(String[] args){
5    File fin,fout;  
6    BufferedReader bf = null;
7    PrintWriter pw = null;
8    try{
9     fin = new File("zzc.txt"); //注意文件与程序都要在同一个文件夹下。zzc.txt为要被复制的文件。
10     fout = new File("copyzzc.txt"); //如果没有会自动创建。
11     bf = new BufferedReader(new FileReader(fin));
12     pw = new PrintWriter(fout); //PrintWriter为打印流,也可以使用BufferedWriter.
13     String line = bf.readLine();
14     while(line!=null){
15     pw.println(line);
16      line = bf.readLine();
17     }
18    }catch(Exception e){
19     e.printStackTrace();
20    }finally{
21     try{
22     //关闭 文件。
23      if(bf!=null){
24       bf.close();
25       bf = null;
26      }
27      if(pw!=null){
28       pw.close();
29       pw = null;
30      }
31     }catch(Exception e){
32      e.printStackTrace();
33     }
34    }
35 }
36 }




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2