import java.io.*;
public class KeyBoardInput
{
public static void main(String args[])
{
String s;
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(ir);
System.out.println("Unix:Type ctrl-d to exit."+"\nWindows:Type ctrl-z to exit");

try
{
s=in.readLine();
while(s!=null)
{
System.out.println("Read:"+s);
s=in.readLine();
}
while((s=in.readLine())!=null);//什么意思呢,为什么要两句while?????
{
System.out.println("Read:"+s);

}
in.close();//为什么要使用这句话呢??

}
catch(IOException e)
{
e.printStackTrace();
}
}
}

解决方案 »

  1.   

    第一个没必要用两个while吧,in.close()这是流要关闭 有关流的输入和输出都要最后关闭的表示输入或输出完信息
      

  2.   

    两个while块是一个意思,应去掉一个
    in.close() 关闭控制台输入流
      

  3.   

    你的代码可以简化成这样package test;
    import java.io.*;
    public class KeyBoardInput
    {
        public static void main(String args[])
        {
            InputStreamReader ir=new InputStreamReader(System.in);
            BufferedReader in=new BufferedReader(ir);
            System.out.println("Unix:Type ctrl-d to exit."+"\nWindows:Type ctrl-z to exit");
        
        try
        {
           String content;
           //while循环的作用就是不断地检测键盘的输入,当你按回车以后程序就认为你的输入已经结束
           //程序会把你刚才的输入打印到控制台。当输入"quit+回车"时,程序将退出
           while(true){
            content=in.readLine();
            if(content.equals("quit")){
            System.out.println("程序即将退出!");
            break;
            }else{
            System.out.println("你输入了:"+content);
            }
           }
           in.close();
        
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        }
    }
    in.close()的作用就是关闭输入流同时释放系统资源,这行执行以后你就不能再通过键盘输入内容了!
      

  4.   

    我的问题是我现在可以在控制台里面不断输入值,但是不输入,也会显示read:+空内容那么in.close(); 有什么用啊?????
      

  5.   

     while((s=in.readLine())!=null);//什么意思呢,为什么要两句while?????
    in这个输入流读取一行放在s对象中, 不等于null判断是否读到最后,至于加while是由于你读的文件不止一行!
            {
                System.out.println("Read:"+s);
                
            }
            in.close();//为什么要使用这句话呢??
    释放流。流这个东西不仅是占用内存(java虚拟机可以垃圾回收),并且占用的io资源(必须手动释放,所以必须clise)。
      

  6.   

    代码质量不高
    in.close()最好也要放到finally{}块中
      

  7.   

    while((s=in.readLine())!=null) //什么意思呢,为什么要两句while?????
           是in里读取一行字直到没有字母的意思。没必要写两句while,
    in.close();
    in是文件吧。 文件打开后一定要用close函数关上。
      

  8.   

    你的程序有两点不妥,第一、就如楼上所说没有必要用两个while,第二in.close()是关闭输入流的意思,但是关闭一般在finally里进行;你写的  while((s=in.readLine())!=null);的意思是不断读取输入的字符流直到为空结束,s=in.readline()是不断的为s赋值,并且判断这个值是否为空
      

  9.   

    谢谢楼上的
    这句代码不懂import java.io.*;
    public class ReadFile
    {
    public static void main(String[] args)
    {
    File file=new File(args[1]);
    try
    {
    BufferedReader in =new BufferedReader(new FileReader(file));
    String s;

    s=in.readLine();
    while(s!=null)
    {
    System.out.println("Reader:"+s);
    in.close();
    }
    }
          catch(FileNotFoundException e1)
    {
    System.err.println("File not found:"+file);
    }
    catch(IOException e2)
    {
    e2.printStackTrace();
    }
    }
    }
      

  10.   

    前两个while完成一样的功能,可以去掉一个!第二个是把读取直接写到while的条件语句,其实和第一条一样,in.close()是关闭流!