Course Content

Course Content

Description

File Input Stream and Console Class in Java

In the previous video of our Core Java course we have seen that what are File Input and Output in Java programming. Now in this video we are going to see that what are FileInputStream and Console class in Java.

FileInputStream Class in Java obtains input bytes from a file. It is used for reading streams of raw bytes such as image data. For reading streams of characters, consider using Filereader. It should be used to read byte oriented data.

Example :
import java.io.*;
class simpleRead
{
public static void main(String args[])
{
try
{
FileInputStream fin-new FileInputStream("abc.txt");
int i=0;
while((i=fin.read ())!=-1
{
System.out.println((char) i);
}
fin.close();
}
catch (Exception e
{
System.out.println(e);
}
}}

The Console Class in Java is used to get input from console. It provides methods to read text and password. If you read password using console class, it will not be displayed to the user. The java.io.console class is attached with system console internally.

Different methods of Console class in Java are as follows :
1. public String readLine() : It is used to read a single line of text from the console.
2. public String readLine(String fmt,Object...args) : it provides of formatted prompt then read the single line of text from the console.
3. public char[] read Password() : it is used to read password that is not being displayed on the console.
4. public char[] readPassword(String fmt,Object...args) : it provides a formatted prompt that reads the password that is not being displayed on.

Example :
import java.io.*;
class ReadTest
{
public static void main(String args[])
{
Console c=System.console();
System.out.println("Enter your name: ");
String n=c.readLine();
System.out.println("Welcome "+n);
}
}
Output :
Enter your name :
hiral
Welcome hiral

So, this is how FileInputStream and Console class in Java works. Now, further we will see that what is Multithreading in Java programming language.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere