IO Streams:
- Stream is a flow of data
- IO streams are used to send and receive the information from Primary to Secondary memory or vice versa.
- java.io package is providing classes and interfaces to implement IO Streams.
Java supports:
- Byte streams
- Character streams
- Buffered Streams
- Data streams
- Console streams
- Object streams (Serialization and De-serialization)
- Array streams
Console Streams:
- Console is a User screen.
- Console object is Fixed and Pre-defined.
- We can read input from the console in CUI(console based) applications.
- java.lang.System class providing functionality to get the console object.
class System
{
public static Console console()
{
Returns Console object.
}
}
Using Console object functionality we can read different types of input.
class Console
{
public String readLine()
{
Reads a single line of text from the console.
}
}
Example code:
import java.io.Console;
class ConsoleRead
{
public static void main(String[] args)
{
Console con = System.console();
System.out.println("Enter any input : ");
String val = con.readLine();
System.out.println("Input is : " + val);
}
}
- In Console based applications, we use authentication(user, password) to work with the application.
- Console object contains readPassword() method.
class Console
{
public char[] readPassword()
{
Read and return password.
}
}
We can construct String object from char[] in many ways.
class String
{
public static String valueOf(char[] data)
{
Converts char[ ] into String object.
}
}
Code program:
import java.io.Console ;
class LoginFile
{
public static void main(String[] args)
{
Console con = System.console();
System.out.print("Enter Username : ");
String uname = con.readLine();
System.out.print("Enter Password : ");
char data[] = con.readPassword();
String pwd = String.valueOf(data);
if(uname.equals("srinivas") && pwd.equals("abc123$"))
System.out.println("Login success...");
else
System.out.println("Authentication failed...");
}
}