Standard streams are the streams that was discussed from the previous chapter.
Console is the advance version of the streams. This is useful for creating and reading secure password. Console uses reader(), writer(), flush(), readPassword() and so on. But before using these methods, you need to create an object using System.console(). Two ways that you cannot access the console:
1. If the OS does not support or allow accessing of the console.
2. If the program was launched in a noninteractive environment.
Why it is secure when it comes to password?
1. It is because it suppress echoing or you cannot see the letters that was type.
2. It uses a character Array and not a string. So, it can overwritten when someone uses it and it is removed from the memory it is not needed.
====================
class PracticingConsole{
public static void main(String[] args){
Console console = System.console();
if(console == null){
System.err.println("No console");
System.exit(1);
}
try(PrintWriter yourWriter = console.writer()){
System.out.println("Enter anything below:");
String message = console.readline();
yourWriter.write(message);
}
try(Scanner scanner = new Scanner(console.reader())){
String gatherItHere = "";
while(scanner.hasNext()){
String message = scanner.nextLine();
gatherItHere = gatherItHere.concat(message);
}
System.out.println(gatherItHere);
}
}
}
===================
Result:
No console
Note:
1. The program has no compile error when it was created. That means all the semantics were correct. The only problem is, I could not access the console.
2. System.exit(1) - if nonzero is put in the argument, that means it needs to terminate the runtime of the JVM because there is an abnormality during closing.
YOU ARE READING
Java Programming
عشوائيhttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...