The Exception is misleading and closing InputStreamReader(System.in) does not allow Console.readLine() to read a line and Console.readPassword() to read password.
For Console.readPassword(), exception message is: IOError :java.io.IOException: The handle is invalid. See Result2:
Closing InputStreamReader(System.in) should not affect Console readLine(),readPassword() methods.
Please see the following code,Result1.
<code>
import java.io.*;
class TestClose4 {
public static void main(String... s){
Console con =System.console();
try{
InputStreamReader reader = new InputStreamReader(System.in);
reader.close();
String st=con.readLine("%s","Enter a line");
System.out.println(st);
}catch(IOException e){
e.printStackTrace();
}catch(IOError error){
error.printStackTrace();
}
}
}
</code>
<Result1>
C:\work>java TestClose4
Enter a linejava.io.IOError: java.io.IOException: No such file or directory
at java.io.Console.readLine(Console.java:236)
at TestClose4.main(TestClose4.java:9)
Caused by: java.io.IOException: No such file or directory
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:199)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.Console$LineReader.read(Console.java:409)
at java.io.Console.readline(Console.java:348)
at java.io.Console.readLine(Console.java:232)
... 1 more
</Result1>
<Result2>
C:\work>java TestClose4
Enter a Passwordjava.io.IOError: java.io.IOException: The handle is invalid
at java.io.Console.readPassword(Console.java:302)
at TestClose4.main(TestClose4.java:10)
Caused by: java.io.IOException: The handle is invalid
at java.io.Console.echo(Native Method)
at java.io.Console.readPassword(Console.java:299)
... 1 more
</Result2>
For Console.readPassword(), exception message is: IOError :java.io.IOException: The handle is invalid. See Result2:
Closing InputStreamReader(System.in) should not affect Console readLine(),readPassword() methods.
Please see the following code,Result1.
<code>
import java.io.*;
class TestClose4 {
public static void main(String... s){
Console con =System.console();
try{
InputStreamReader reader = new InputStreamReader(System.in);
reader.close();
String st=con.readLine("%s","Enter a line");
System.out.println(st);
}catch(IOException e){
e.printStackTrace();
}catch(IOError error){
error.printStackTrace();
}
}
}
</code>
<Result1>
C:\work>java TestClose4
Enter a linejava.io.IOError: java.io.IOException: No such file or directory
at java.io.Console.readLine(Console.java:236)
at TestClose4.main(TestClose4.java:9)
Caused by: java.io.IOException: No such file or directory
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:199)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.Console$LineReader.read(Console.java:409)
at java.io.Console.readline(Console.java:348)
at java.io.Console.readLine(Console.java:232)
... 1 more
</Result1>
<Result2>
C:\work>java TestClose4
Enter a Passwordjava.io.IOError: java.io.IOException: The handle is invalid
at java.io.Console.readPassword(Console.java:302)
at TestClose4.main(TestClose4.java:10)
Caused by: java.io.IOException: The handle is invalid
at java.io.Console.echo(Native Method)
at java.io.Console.readPassword(Console.java:299)
... 1 more
</Result2>
- relates to
-
JDK-6347283 Console.writer/readLine/rePassword does not work after closing the System.out/in
-
- Closed
-