日期:2014-05-20 浏览次数:20770 次
public class test {
public static void main(String[]args) throws Exception{
String s2 = readScanner("请输入:");
System.out.println("这是你Scanner输入的:"+s2);
String str = readString("请输入:");
System.out.println("这是你输入的"+str);
}
private static String readString(String prompt) throws IOException{
String str = null ;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print(prompt);
str = br.readLine() ;
return str ;
}
private static String readScanner(String prompt){
String retStr = null ;
System.out.print(prompt);
Scanner scanner = new Scanner(System.in);
retStr = scanner.nextLine();
scanner.close();
return retStr ;
}
}
请输入:ggreg
这是你Scanner输入的:ggreg
请输入:Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at test.readString(test.java:20)
at test.main(test.java:13)