- import java.io.*;
- class BufferCopy
- {
- public static void main(String[] args)
- {
- BufferedWriter bufw=null;
- BufferedReader bufd =null;
- try
- {
- bufd = new BufferedReader(new FileReader("FileWriterDemo3.java")); //关联要读取的文件文件
- bufw = new BufferedWriter(new FileWriter("FileWriterDemo_copy.txt")); //需要创建的文件
-
- String line = null;
- while((line=bufd.readLine())!=null)
- {
- bufw.write(line);
- bufw.newLine();
- bufw.flush();
- }
- }
- catch(IOException e)
- {
- throw new RuntimeException("读取异常");
- }
- finally
- {
- try
- {
- if(bufw!=null)
- bufw.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException("读取异常");
- }
- try
- {
- if(bufd!=null)
- bufd.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException("读取异常");
- }
- }
-
- }
- }
复制代码 就是用缓冲区复制文件,如果读取的文件和这段代码保存在同一个目录下,就没问题。但是没在同一个目录下就会报读取异常 ,这是怎么回事?
|
|