Course Content

Course Content

Description

File Input and Output in Java

In the previous video we have seen that what are the types of Exceptions in Java programming and how Exception handling in Java actually works with an example. Now, in this video we are going to see that what are File Input Output in Java.

File in Java Programming is a collection of record. Suppose, a song folder contains old songs, new songs, middle songs etc. For one particular old song, we can search it in old song folder. Same way in java, I/O is used to process the input and procedure output based on input. Java uses Stream concept for making I/O operation fast.

The java.io package contains all the classes required for input and output operations. This is an in built packages. You have to define java.io in your program. We perform file handling in java through I/O API. API is application program interface.

Stream : A Stream is sequence of data. In java, a stream is composed of Bytes. Stream is like running water, continuously flowing. That is why it is called Stream in Java programming.
There are two types of streams :
Input Stream in Java : it is used for reading the data from a source.
Output Stream in Java : the Output stream is used for writing data to a destination.

Input Stream Class in Java : It is an Abstract class. Abstract class is a class without definition. In this, abstract keyword is compulsory. It is the superclass of all classes representing an input stream of bytes. There are three methods of this :
1. Public abstract int read() throws IOException – read the next byte of data from the input stream. At the end of file, it returns -1.
2. Public int available() throws IOException – return an estimate of the number of bytes that can be read from the current stream.
3. Public void close() throws IOException – it is used to close the current input stream.

Output Stream Class in Java : It is an Abstract class. It is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to same sink. There are four methods of this:
1. Public void write(int)throws IOException – it is used to write a byte to the current output stream.
2. Public void write(byte[])throws IOexception – it is used to write an array of byte to the current output stream.
3. Public void flush()throws IOException – Flushes the current output stream.
4. Public void close()throws IOException – it is used to close the current output stream.

Java FileOutputStream : It is an output stream for writing data to a file. It writes primitive values. Primitive values means pure value. To write primitive values, FileOutputStream is used. While for character oriented data, FileWriter is used. You can write byte-oriented and character-oriented data as well. Let’s see example :
Import java.io.*;
Class Testt
{
Public static void main(String args[])
{
Try
{
FileOutputStream fout= new FileOutputStream( “abc.txt”);
String s= “Sachin Tendulkar is my favorite player;
Byte b[]=s.getBytes(); //converting string into byte array
Fout.write(b);
Fout.close();
System.out.println(“success…”);
}
Catch(Exception e) {System.out.println(e);
}
}
}

Java FileOutputStream : It is obtains input bytes from a file. It is used for reading streams of raw bytes such as image data. FileReader is used for reading streams of characters. Also, we can read byte-oriented data from image, audio & video.
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);
}
}
}
Output :
Sachin Tendulakar is….

Console Class in Java : The Java Console class is used to get Input from console. It provides methods to read text and password. If you can read password using Console class, it will not be displayed to user. We can get this from java.io.console class. It is attached with system console internally.
There are four methods of this :
1. Public String readLine() –It is used to read a single line of text from the console.
2. Public String readLine(Stringfmt, Object…args) – It provides a 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 display on the console.
4. Public char[] readPassword(Stringfmt, Object…args) – it provides a formatted prompt then reads the password that is not being display on.

Example for Console class in Java programming :
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);
}
}

So, this is how File Input and Output in Java programming language works and also we have seen Java FileOutputStream. In the next video we will see that what are FileInputStream and Console Class in Java programming.

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