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();
}
}
}
in.close() 关闭控制台输入流
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()的作用就是关闭输入流同时释放系统资源,这行执行以后你就不能再通过键盘输入内容了!
in这个输入流读取一行放在s对象中, 不等于null判断是否读到最后,至于加while是由于你读的文件不止一行!
{
System.out.println("Read:"+s);
}
in.close();//为什么要使用这句话呢??
释放流。流这个东西不仅是占用内存(java虚拟机可以垃圾回收),并且占用的io资源(必须手动释放,所以必须clise)。
in.close()最好也要放到finally{}块中
是in里读取一行字直到没有字母的意思。没必要写两句while,
in.close();
in是文件吧。 文件打开后一定要用close函数关上。
这句代码不懂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();
}
}
}