黑马程序员技术交流社区

标题: IO读取失败 [打印本页]

作者: 杨庆雷    时间: 2014-4-16 23:55
标题: IO读取失败
本帖最后由 杨庆雷 于 2014-4-17 16:49 编辑

package IO;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReadWrite {

        public static void main(String[] args) {
                // TODO Auto-generated method stub
                copy();
        }
        
        
        public static void copy(){
                FileWriter fw = null;
                FileReader fr = null;
               
                try {
                        fw = new FileWriter("E:\\Demo.txt");
                        fr = new FileReader("F:\\Demo2.txt");
                        char[] buf = new char[1024];
                        int len = 0 ;
                        while((len = fr.read(buf))!=-1){
                                fw.write(buf,0,len);
                        }
                        
                } catch (IOException e) {
                        throw new RuntimeException("读取异常");
                }
                finally{
                        if(fw != null)
                        try {
                                fw.close();
                        } catch (IOException e) {
                                
                        }
                        
                        if(fr != null)
                        try {
                                fr.close();
                        } catch (IOException e) {
                                
                        }
                }
               
        }

}
读取失败 我在eclipse上 debug 的时候 会在 21行跳出错  请问是怎么回事

debug.png (11.21 KB, 下载次数: 57)

debug时

debug时

aaaa.png (8.4 KB, 下载次数: 25)

aaaa.png

作者: 别小乐    时间: 2014-4-17 00:18
楼主,我测试了5遍,没有任何问题。能不能把异常完整的发出来看看。
作者: 向日葵的曙光    时间: 2014-4-18 07:37
fw = new FileWriter("E:\\Demo.txt");
fr = new FileReader("F:\\Demo2.txt");
你这可能是路劲不对,找不到E:\\Demo.txt"文件,如果文件不存在,写操作就不知道往哪里写入
所以必须保证路径是正确并且存在的




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