Java I/O
Java I/O :
Java I/O (Input and Output) is used to process the input
and produce the output.
Java uses the concept of a stream to make I/O operation
fast. The java.io package contains all the classes required
for input and output operations.
We can perform file handling in Java by Java I/O API.
Java I/O
Stream :
A stream is a sequence of data. In Java, a stream is
composed of bytes. It's called a stream because it is like a
stream of water that continues to flow.
In Java, 3 streams are created for us automatically. All
these streams are attached with the console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
Java I/O
Stream :
Let's see the code to print output and an error message to the
console.
System.out.println("simple message");
System.err.println("error message");
Let's see the code to get input from console.
int i=System.in.read();//returns ASCII code of 1st character
System.out.println((char)i);//will print the character
Java I/O
OutputStream vs InputStream :
OutputStream :
Java application uses an output stream to write data to a
destination; it may be a file, an array, peripheral device or
socket.
InputStream :
Java application uses an input stream to read data from a
source; it may be a file, an array, peripheral device or socket.
Java I/O
Working of Java OutputStream and InputStream
Java I/O
OutputStream class :
OutputStream class 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 some sink.
Method Description
1) public void write(int)throws IOException is used to write a byte to the
current output stream.
2) public void write(byte[])throws IOException 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 is used to close the current
output stream.
Java I/O
OutputStream Hierarchy
Java I/O
InputStream class
InputStream class is an abstract class. It is the superclass of all
classes representing an input stream of bytes.
Method Description
1) public abstract int read()throws reads the next byte of data from
IOException the input stream. It returns -1 at the
end of the file.
2) public int available()throws IOException returns an estimate of the number
of bytes that can be read from the
current input stream.
3) public void close()throws IOException is used to close the current input
stream.
4) public void close()throws IOException is used to close the current output
stream.
Java I/O
InputStream Hierarchy
Java I/O
FileOutputStream class declaration
The declaration for Java.io.FileOutputStream class:
public class FileOutputStream extends OutputStream
FileOutputStream class methods :
Method Description
protected void finalize() It is used to clean up the connection
with the file output stream.
void write(byte[] ary) It is used to write ary.length bytes from
the byte array to the file output stream.
void write(byte[] ary, int off, int len) It is used to write len bytes from the
byte array starting at offset off to the
file output stream.
Java I/O
FileOutputStream class methods :
Method Description
void write(int b) It is used to write the specified byte to the file
output stream.
FileChannel getChannel() It is used to return the file channel object
associated with the file output stream.
FileDescriptor getFD() It is used to return the file descriptor associated
with the stream.
void close() It is used to closes the file output stream.
Java I/O
Java FileOutputStream Example 1: write byte
import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new
FileOutputStream("D:\\testout.txt");
fout.write(65);
fout.close();
System.out.println("success...");
}catch(Exception e){System.out.println(e);}
}}
Output:
Success...
Java I/O
Java FileOutputStream Example 1: write byte
The content of a text file testout.txt is set with the data A.
testout.txt
A
Java I/O
Java FileOutputStream example 2: write string
import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new
FileOutputStream("D:\\testout.txt");
String s="Welcome to KIIT.";
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 I/O
Java FileOutputStream example 2: write string
Output:
Success...
The content of a text file testout.txt is set with the data Welcome
to javaTpoint.
testout.txt
Welcome to KIIT.
Java I/O
Java FileInputStream Class
Java FileInputStream class obtains input bytes from a file. It is
used for reading byte-oriented data (streams of raw bytes) such
as image data, audio, video etc. You can also read
character-stream data. But, for reading streams of characters, it
is recommended to use FileReader class.
The declaration for java.io.FileInputStream class:
public class FileInputStream extends InputStream
Java I/O
Java FileInputStream Class
Method Description
int available() It is used to return the estimated number
of bytes that can be read from the input
stream.
int read() It is used to read the byte of data from
the input stream.
int read(byte[] b) It is used to read up to b.length bytes of
data from the input stream.
int read(byte[] b, int off, int len) It is used to read up to len bytes of data
from the input stream.
long skip(long x) It is used to skip over and discards x
bytes of data from the input stream
Java I/O
Java FileInputStream Class
Method Description
FileChannel getChannel() It is used to return the unique
FileChannel object associated with the
file input stream.
FileDescriptor getFD() It is used to return the FileDescriptor
object.
protected void finalize() It is used to ensure that the close
method is call when there is no more
reference to the file input stream.
void close() It is used to closes the stream.
Java I/O
Java FileInputStream example 1: read single character
import java.io.FileInputStream;
public class DataStreamExample {
public static void main(String args[]){
try{
FileInputStream fin=new
FileInputStream("D:\\testout.txt");
int i=fin.read();
System.out.print((char)i);
fin.close();
}catch(Exception e){System.out.println(e);}
}
}
Java I/O
Java FileInputStream example 1: read single character
Note: Before running the code, a text file named as "testout.txt"
is required to be created. In this file, we are having following
content:
Welcome to KIIT.
After executing the above program, you will get a single
character from the file which is 87 (in byte form). To see the text,
you need to convert it into character.
Output:
W
Java I/O
Java FileInputStream example 2: read all characters
import java.io.FileInputStream;
public class DataStreamExample {
public static void main(String args[]){
try{
FileInputStream fin=new
FileInputStream("D:\\testout.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.print((char)i);
}
fin.close();
}catch(Exception e){System.out.println(e);}
}}
Java I/O
Java FileInputStream example 2: read all characters
Output:
Welcome to KIIT.
Java I/O
PrintWriter class :
Java PrintWriter class is the implementation of Writer class. It is
used to print the formatted representation of objects to the
text-output stream.
Practice few example using PrintWriter class.