我写了一段拷贝文件的代码,代码如下:- import java.io.*;
- class FileWork
- {
- private String sourceName, destName;
- private BufferedReader read;
- private BufferedWriter write;
- private String line;
-
- private boolean openFile()
- {
- try
- {
- read = new BufferedReader(new FileReader(sourceName));
- }
- catch(IOException e)
- {
- System.out.println("OpenFileException:"+sourceName);
- return false;
- }
-
- try
- {
- write = new BufferedWriter(new FileWriter(destName));
- }
- catch(IOException e)
- {
- System.out.println("OpenFileException:"+destName);
- return false;
- }
- return true;
- }
-
- private boolean copyFile()
- {
- int count = 0;
- try
- {
- line = read.readLine();
- while(line != null)
- {
- write.write(line);
- write.newLine();
- line = read.readLine();
- }
- }
- catch(IOException e)
- {
- count++;
- if(count > 0)
- System.out.println("Find "+count+" IOException");
- }
- return true;
- }
-
- private boolean closeFile()
- {
- boolean flag = true;
- try
- {
- read.close();
- }
- catch(IOException e)
- {
- System.out.println("CloseFileException:"+sourceName);
- flag = false;
- }
- try
- {
- write.close();
- }
- catch(IOException e)
- {
- System.out.println("CloseFileException:"+destName);
- flag = false;
- }
- return flag;
- }
-
- public boolean work(String src, String dst)
- {
- sourceName = src;
- destName = dst;
-
- return openFile()&©File()&&closeFile();
- }
- }
复制代码 我测试的时候,先拷贝了一个8.63M的.out文件,没有问题,之后我又拷贝了一个135M的.out文件结果提示OutoffMemeryException异常。然后我查了一下关于BufferedReader的信息,它的默认大小是8K,那么也就是说它并不是一次就存取完的,是份多次存取的,那如果是这样的话为什么还有内存异常呢?请给出原因和解决办法,感谢给位大牛!
|
|