A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李玉杰 黑马帝   /  2012-2-9 14:52  /  2455 人查看  /  12 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 lyj749 于 2012-2-9 17:40 编辑

public class Copy {
        public static void main(String[] args) throws IOException {
       
                copy_2();

                }       

        private static void copy_2() throws IOException  {

                FileWriter W=new FileWriter("123.txt");
                W.write("abc");
                W.flush();
                FileReader Y=new FileReader("123.txt");       
                while (Y.read()!=-1) {
                        System.out.print(Y.read());       
                }
                Y.close();
                W.close();
        }
}
我写的一个IO的问题运行完了没看懂,为什么结果是98-1 ??

评分

参与人数 1技术分 +1 收起 理由
荣凯旋 + 1

查看全部评分

12 个回复

倒序浏览
本帖最后由 彭沛东 于 2012-2-9 15:00 编辑
  1. public static void main(String[] args) {
  2.                 FileWriter W = null;
  3.                 FileReader Y = null;
  4.                 try {
  5.                         W = new FileWriter("123.txt");
  6.                         W.write("abc");
  7.                         W.flush();
  8.                         Y = new FileReader("123.txt");
  9.                         int leng = 0;
  10.                         while ((leng = Y.read()) != -1) {
  11.                                 System.out.println((char) leng);
  12.                         }
  13.                 } catch (IOException e) {
  14.                         e.printStackTrace();
  15.                 } finally {
  16.                         try {
  17.                                 if (W != null) {
  18.                                         W.close();
  19.                                 }
  20.                                 if (Y != null) {
  21.                                         Y.close();
  22.                                 }
  23.                         } catch (IOException e) {
  24.                                 e.printStackTrace();
  25.                         }
  26.                 }
  27.         }
复制代码
因为读出来的是byte数据、需要转成char。
这是我修正过的。

评分

参与人数 1技术分 +1 收起 理由
荣凯旋 + 1

查看全部评分

回复 使用道具 举报
没有经过转换
import java.io.*;
public class JavaTest {
         public static void main(String[] args) throws IOException {
         
                copy_2();

                }        

        private static void copy_2() throws IOException  {

                FileWriter W=new FileWriter("c:\\a\\123.txt");
                 W.write("abc");
                 W.flush();
                 FileReader Y=new FileReader("c:\\a\\123.txt");      
                                 int temp = 0;
                while ((temp=Y.read())!=-1) {
                         System.out.print((char)temp);        
                }
                 Y.close();
                 W.close();
         }
}
回复 使用道具 举报
彭沛东 发表于 2012-2-9 14:58
因为读出来的是byte数据、需要转成char。
这是我修正过的。

我没让你修正,我问为什么结果是98  -1....
回复 使用道具 举报
本帖最后由 彭沛东 于 2012-2-9 15:27 编辑
lyj749 发表于 2012-2-9 15:02
我没让你修正,我问为什么结果是98  -1....


FileWriter 用于写入字符流。
回复 使用道具 举报
彭沛东 发表于 2012-2-9 15:04
因为你是用字节流读取的、所以打印是字节啊。

照你这么说那为什么不是97 98 99?
回复 使用道具 举报
lyj749 发表于 2012-2-9 15:10
照你这么说那为什么不是97 98 99?

这个这个我也想知道了。
回复 使用道具 举报
王_涛 黑马帝 2012-2-9 15:30:04
8#
while (Y.read()!=-1) {
                        System.out.print(Y.read());        
     }
问题发生在这里;为什么不是979899
因为你一次循环调用了2次read方法
1.在Y.read()!=-1调用了一次 //没有输出
2.在System.out.print(Y.read()); 又调用了一次 //输出了

结果是就 97没输出 98输出
         99没输出 -1输出

结果是98-1

评分

参与人数 1技术分 +1 收起 理由
唐秀启 + 1

查看全部评分

回复 使用道具 举报
王_涛 发表于 2012-2-9 15:30
while (Y.read()!=-1) {
                        System.out.print(Y.read());        
     }

:handshake、这都被你发现了。
回复 使用道具 举报
刘丁 黑马帝 2012-2-9 15:35:24
10#
  private static void copy_2() throws IOException  {

                FileWriter W=new FileWriter("123.txt");
                W.write("abc");
                W.flush();
                FileReader Y=new FileReader("123.txt");
                int temp;
                while ((temp =Y.read())!=-1) {
                        System.out.print((char)temp);        
                }
                Y.close();
                W.close();
        }

原因在从文件中读的时候做判断时你读了一次,当你写到文件中的时候,你又读了一次,这时写到文件中的数就不是你刚开妈做判断时的数据了。这样数据就不完整了。 还有在写入手时要做一个转换。。
回复 使用道具 举报

画真相让你了然

评分

参与人数 1技术分 +1 收起 理由
唐秀启 + 1

查看全部评分

回复 使用道具 举报
while (Y.read()!=-1) {  //1.第一次循环:Y.read()='a',不等于-1; //3.第二次循环:Y.read()='c',不等于-1
                        System.out.print(Y.read());        //2.接着你再次调用Y.read(),此时读取到第二个字符‘b’,打印:98。 //4.再次调用Y.read(),此时已经到文件末尾,打印:-1,再次判断后,循环结束。
                }                                                   
回复 使用道具 举报
public class Copy {
    public static void main(String[] args) throws IOException {
   
            copy_2();

            }        

    private static void copy_2() throws IOException  {

            FileWriter W=new FileWriter("d:"+File.separator+"abc.txt");
            W.write("abc");
            W.flush();
            FileReader Y=new FileReader("d:"+File.separator+"abc.txt");     
            int temp = 0 ;
            while ((temp=Y.read())!=-1) {
                    System.out.print(temp);        
            }
            Y.close();
            W.close();
    }
}
输出结果:979899
起初输出结果是98-1可能是因为在Y.read()!=-1的时候只是读取了文件中的内容,但没有把内容保存起来,因为
要想实现边读边写,就应该先把读取出来的值保存起来,然后在输出。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马