黑马程序员技术交流社区

标题: 关于IO流读取问题 [打印本页]

作者: ″先森丶玹°    时间: 2013-11-29 18:09
标题: 关于IO流读取问题
本帖最后由 ″先森丶玹° 于 2013-11-30 22:05 编辑
  1.         
  2.         public static void writeCode() throws IOException {

  3.                 OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream("D:\\IO测试专用\\其他流\\change.txt"),"GBK");
  4.                
  5.                 osw.write("编程思想");
  6.                
  7.                 osw.close();
  8.                
  9.         }
  10.         
  11.         public static void readCode() throws IOException {
  12.                 InputStreamReader isr=new InputStreamReader(new FileInputStream("D:\\IO测试专用\\其他流\\change.txt"),"GBK");
  13.                
  14.                 char[] buf=new char[1024];
  15.                
  16.                 int length=isr.read(buf);
  17.                
  18.                 sop(new String(buf,0,length));
  19.                
  20.                 isr.close();
  21.         }
复制代码


问题:为什么int length=isr.read(buf);
               
                sop(new String(buf,0,length));
这里不需要用while循环来读取?
while((length.read(buf))!=-1){
sop(new String(buf,0,length));}
这样的
作者: HAnG    时间: 2013-11-29 18:19
一般都要while的,是你这个例子因为文本文件里就4个汉字,为了省事没写吧。
作者: 冯晓骏    时间: 2013-11-29 19:16
就这么几个字节,一次就都读取到了
作者: 简★零度    时间: 2013-11-30 20:39
问题解决了就把类型改成提问结束1




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