黑马程序员技术交流社区

标题: IO问题 [打印本页]

作者: 李玉杰    时间: 2012-2-9 14:52
标题: IO问题
本帖最后由 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 ??
作者: 彭沛东    时间: 2012-2-9 14:58
本帖最后由 彭沛东 于 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。
这是我修正过的。

作者: 任奇    时间: 2012-2-9 14:59
没有经过转换
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 15:02
彭沛东 发表于 2012-2-9 14:58
因为读出来的是byte数据、需要转成char。
这是我修正过的。

我没让你修正,我问为什么结果是98  -1....
作者: 彭沛东    时间: 2012-2-9 15:04
本帖最后由 彭沛东 于 2012-2-9 15:27 编辑
lyj749 发表于 2012-2-9 15:02
我没让你修正,我问为什么结果是98  -1....


FileWriter 用于写入字符流。
作者: 李玉杰    时间: 2012-2-9 15:10
彭沛东 发表于 2012-2-9 15:04
因为你是用字节流读取的、所以打印是字节啊。

照你这么说那为什么不是97 98 99?
作者: 彭沛东    时间: 2012-2-9 15:23
lyj749 发表于 2012-2-9 15:10
照你这么说那为什么不是97 98 99?

这个这个我也想知道了。
作者: 王_涛    时间: 2012-2-9 15:30
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
作者: 彭沛东    时间: 2012-2-9 15:32
王_涛 发表于 2012-2-9 15:30
while (Y.read()!=-1) {
                        System.out.print(Y.read());        
     }

:handshake、这都被你发现了。
作者: 刘丁    时间: 2012-2-9 15:35
  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();
        }

原因在从文件中读的时候做判断时你读了一次,当你写到文件中的时候,你又读了一次,这时写到文件中的数就不是你刚开妈做判断时的数据了。这样数据就不完整了。 还有在写入手时要做一个转换。。
作者: 余海龙    时间: 2012-2-9 15:38

画真相让你了然
作者: 刘基军    时间: 2012-2-9 15:40
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,再次判断后,循环结束。
                }                                                   
作者: 何洪森    时间: 2012-2-9 15:45
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的时候只是读取了文件中的内容,但没有把内容保存起来,因为
要想实现边读边写,就应该先把读取出来的值保存起来,然后在输出。




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