Introduction
This document introduces the basic concepts of java File I/O package.
What if you want to access a file through code anytime?
When you develop any java application then you will get the requirement to interact with various input/output devices. Input-Output devices are the part of underlying hardware and operating system. To interact with the Input-Output devices through java program/application some implementations had to be used to improve the maintenance of applications when someone changes device or OS.
Watch this Java Tutorial for Beginners by Intellipaat:
To resolve this, java vendor provided API in java.io package in which each class represents one source or device. This tutorial will help you to understand about the java I/O package.
What is File I/O?
Java I/O stream is the flow of data that you can either read from, or you can write to.
It is used to perform read and write operations in file permanently. Java uses streams to perform these tasks. Java I/O stream is also called File Handling, or File I/O. It is available in java.io package.
Java.io package provides classes for system input and output through files, network streams, memory buffers, etc.
Some input-output stream will be initialized automatically by the JVM and these streams are available in System class as in, out, and err variable.
- In reference refers to the default input device, i.e. keyboard.
- Out and err refers to the default output device, i.e. console.
Get 100% Hike!
Master Most in Demand Skills Now!
Streams:
Streams are the sequence of bits(data).
There are two types of streams:
- Input Streams
- Output Streams
Input Streams:
Input streams are used to read the data from various input devices like keyboard, file, network, etc.
Output Streams:
Output streams are used to write the data to various output devices like monitor, file, network, etc.
Streams based on data
There are two types of streams based on data:
- Byte Stream: used to read or write byte data.
- Character Stream: used to read or write character data.
Byte Input Stream:
- These are used to read byte data from various input devices.
- InputStream is an abstract class and it is the super class of all the input byte streams.
List of Byte Input Streams:
Byte Output Stream:
- These are used to write byte data to various output devices.
- Output Stream is an abstract class and it is the superclass for all the output byte streams.
List of Byte Output Streams:
Character Input Stream:
- These are used to read char data from various input devices.
- Reader is an abstract class and is the super class for all the character input streams.
List of Character Input Streams:
Character Output Stream:
- These are used to write char data to various output devices.
- Writer is an abstract class and is the super class of all the character output streams.
List of Character Output Stream:
Example to read contents of file:
Example 1:
import java.io.*;
class ReadHello{
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream(“hello.txt”);
int i=0;
while((i=fin.read())!=-1){
System.out.println((char)i);
}
fin.close();
} catch(Exception e){
system.out.println(e);}
}
}
}
Refer write file example to see contents of hello.txt
Output:
Hello Intellipaat
Example 2:
import java.io.*;
public class ReadFileDemo {
public static void main(String[] args) {
BufferedReader br = null;
BufferedReader br2 = null;
try{
br = new BufferedReader(new FileReader(“B:\\myfile.txt”));
String contentLine = br.readLine();
while (contentLine != null) {
System.out.println(contentLine);
contentLine = br.readLine();}
}
catch (IOException ioe)
{
ioe.printStackTrace();}
}
Output:
Now you know how to create & read
Example to write in a file:
Example 1:
import java.io.*;
public class Intellipaat{
public static void main(String args[]){
try{
FileOutputstream fo=new FileOutputStream(“hello.txt”);
String i=”Hello Intellipaat “;
byte b[]=i.getBytes();/ /converting string into byte array
fo.write(i);
fo.close();
}
catch(Exception e){
system.out.println(e);}
}
}
This will create a file hello.txt
Output:
Hello Intellipaat
Example 2:
import java.io.*;
public class writeIntellipaatFirst{
public static void main(String args[]){
File file=new File(“visiters.txt”);
try{
PrintWriter output=new PrintWriter(file);
output.println(“Hello Visiters! Welcome to Intellipaat”);
output.close();
} catch(IOException ex){ System.out.println(“Error:”+ex);}
}
}
Similarly, different subclasses can also be used to read and write.
CONCLUSION
Input and Output process using standard java methods are somewhat more complex as compared to java I/O. By using, understanding and experimenting with each of the concepts mentioned here, you will be able to perform some of the most common Input-Output operations on the java applications.