黑马程序员技术交流社区

标题: 关系IO流复制问题2 [打印本页]

作者: 自由自在2014    时间: 2014-2-9 18:14
标题: 关系IO流复制问题2
FileInputStream fr=new FileInputStream("E:\\a.txt");
               FileOutputStream fw= new FileOutputStream("E:\\b.txt");
      
            while (fr.read()!=-1){
            fw.write(fr.read());  //这里的fr.read()返回的是数字 为什么不需要char强制转换就能到b文件内?
            }  
              fr.close();  
               fw.close();
         a文件中是abcd 复制的b文件中是bd 这是为什么?
             
作者: lixiuliang    时间: 2014-2-9 18:25
楼主上一个帖子已经有人帮你纠正过这个错误了

FileInputStream fr=new FileInputStream("E:\\a.txt");
               FileOutputStream fw= new FileOutputStream("E:\\b.txt");
      
            while (fr.read()!=-1){//你这里read了一次,没储存
            fw.write(fr.read());  //这里read了一次,储存了
            }  
              fr.close();  
               fw.close();

读取a没储存,然后读取b储存,读取c没储存,读取d储存。最后b文件中是bd
作者: 自由自在2014    时间: 2014-2-9 18:41
lixiuliang 发表于 2014-2-9 18:25
楼主上一个帖子已经有人帮你纠正过这个错误了

FileInputStream fr=new FileInputStream("E:\\a.txt");

那要怎么用while判断来实现全部存储
作者: lixiuliang    时间: 2014-2-9 18:45
自由自在2014 发表于 2014-2-9 18:41
那要怎么用while判断来实现全部存储

int temp;
while((temp = fr.read()) != -1){
        fw.write(temp);
}
试试行不
作者: 自由自在2014    时间: 2014-2-9 18:53
lixiuliang 发表于 2014-2-9 18:45
int temp;
while((temp = fr.read()) != -1){
        fw.write(temp);

那样出错了 改成下面的运行成功了

int temp=fr.read();
            while ( m!=-1){
                    fw.write(m);
                    temp=fr.read();
            }  
             
作者: lixiuliang    时间: 2014-2-9 19:26
自由自在2014 发表于 2014-2-9 18:53
那样出错了 改成下面的运行成功了

int temp=fr.read();

:o 这是什么
作者: 自由自在2014    时间: 2014-2-10 13:49
lixiuliang 发表于 2014-2-9 19:26
这是什么



int temp=fr.read();
            while ( temp!=-1){
                    fw.write(temp);
                    temp=fr.read();
            }  
            
作者: 孤独的天奇    时间: 2014-2-11 11:17
int temp;
while ( (temp=fr.read())!=-1){
        fw.write(temp);
}  




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