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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 442851994 中级黑马   /  2012-11-1 10:47  /  1405 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 442851994 于 2012-11-1 18:13 编辑

package cn.itcast;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class fuxi7 {
        public static void main(String[] args) throws IOException {
                        InputStream is=System.in;
                        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("fw.txt"));
                        int len=0;
                        StringBuilder sb=new StringBuilder();
                        while(true){
                                //System.out.println(is.read());
                                
                                len=is.read();
                                if(len=='\r'){
                                        continue;
                                }
                                if(len=='\n'){
                                         
                                        bos.write(sb.toString().getBytes());
                                        sb.delete(0, sb.length());
                                }else{
                                        sb.append((char)len);
                                }
                        }
                        bos.close();//这句话会报错,提示出现remove,就将我这句关闭流的话删掉了??
        }

}

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

3 个回复

倒序浏览
本帖最后由 都彭韬 于 2012-11-1 11:14 编辑

因为你 while() 循环中 写的是死循环 你这样写就好了
  1. public class fuxi7 {
  2.                 public static void main(String[] args) throws IOException {
  3.                                 InputStream is=System.in;
  4.                                 BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("fw.txt"));
  5.                                 int len=0;
  6.                                 StringBuilder sb=new StringBuilder();
  7.                                 boolean b=true;
  8.                                 while(is.read()!=-1){//这样写更好
  9.                                         //System.out.println(is.read());
  10.                                         
  11.                                         len=is.read();
  12.                                         if(len=='\r'){
  13.                                                 continue;
  14.                                         }
  15.                                         if(len=='\n'){
  16.                                                  
  17.                                                 bos.write(sb.toString().getBytes());
  18.                                                 sb.delete(0, sb.length());
  19.                                         }else{
  20.                                                 sb.append((char)len);
  21.                                         }
  22.                                 }
  23.                                 bos.close();//不会报错啦
  24.                                
  25.                 }
复制代码

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
while(true)  是个死循环,它后面的代码是不会被执行的,所有无论你打什么代码,都会报错的,可以改成这样。
  1. public class fuxi7 {
  2.         public static void main(String[] args) throws IOException {
  3.                         InputStream is=System.in;
  4.                        
  5.                         BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("fw.txt"));
  6.                         int len=0;
  7.                         StringBuilder sb=new StringBuilder();
  8.                         while(is.read()!=-1){
  9.                                 //System.out.println(is.read());
  10.                                 
  11.                                 len=is.read();
  12.                                 if(len=='\r'){
  13.                                         continue;
  14.                                 }
  15.                                 if(len=='\n'){
  16.                                          
  17.                                         bos.write(sb.toString().getBytes());
  18.                                         sb.delete(0, sb.length());
  19.                                 }else{
  20.                                         sb.append((char)len);
  21.                                 }
  22.                                 
  23.                                 
  24.                         }
  25.                        
  26.                         is.close();
  27.                         bos.close();
  28.         }

  29. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
循环条件改成用变量表示,试试
boolean flag=true;
while(flag)
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马