File Input /
Output in JAVA
Instructor Name: Mr. Abrar Ahmad
Department of Computer Science, HITEC University Taxila - Pakistan
Outline
2
Files and I/O
What is Stream
Kinds of Streams
Byte and Character Stream
Writing and Reading from Files
References
Files and I/O
3
• Java uses the concept of a stream to make I/O operation fast.
• Streams represent an input source and an output destination.
• We can perform file handling in Java by Java I/O API.
• java.io package contains all the classes required for input and output operations.
What is Stream
4
• A stream is a sequence of data.
• In Java, streams are used to perform input and output operations
in a sequential manner.
• In Java, a stream is composed of bytes.
• There are 3 streams created automatically, see the example below.
System.out.println("simple message"); //will print the simple message
System.err.println("error message"); //will print the error message
int i=System.in.read();//returns ASCII code of 1st character
System.out.println((char)i);//will print the character
System.out.println(i);//will print the ASCII code
What is a Stream?
5
• A stream is a sequence of data.
• In Java, streams are used to perform input and output operations
in a sequential manner.
• There are two classes for Streams:
• InPutStream − The InputStream is used to read data from a source.
• OutPutStream − The OutputStream is used for writing data to a destination.
Output Stream vs Input Stream
6
• An output stream to write data to a destination.
• An input stream to read data from a source
• It may be a file, an array, peripheral device or socket
Output Stream
7
• 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.
Useful methods of Output Stream
8
Method Description
1) public void write(int) throws IOException is used to write a byte to the current output stream.
is used to write an array of byte to the current output
2) public void write(byte[]) throws IOException
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.
Input Stream
9
• InputStream class is an abstract class.
• It is the superclass of all classes representing an input stream of bytes.
Useful methods of Input Stream
10
Method Description
reads the next byte of data from the input stream. It
1) public abstract int read() throws IOException
returns -1 at the end of the file.
returns an estimate of the number of bytes that can be
2) public int available() throws IOException
read from the current input stream.
3) public void close() throws IOException is used to close the current input stream.
11
Byte Streams
12
Byte Streams
• When an I/O stream manages 8-bit bytes of raw binary data, it is called a byte stream
• When the file format is non-textual (e.g., images, videos, executable
files).
• Many classes related to byte streams.
• Most frequently used classes are, FileOutputStream and FileInputStream.
Java FileOutputStream Class (Example write byte)
13
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(); Output:
System.out.println("success..."); Success...
}
The content of a text file testout.txt is set with
catch(IOException e) the data A.
{System.out.println(e);}
} testout.txt
}
A
Java FileOutputStream Class (Example write string)
14
import java.io.FileOutputStream;
public class FileOutputStreamExample
{
public static void main(String args[])
{
try
{
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
String s="Welcome Class";
byte b[]=s.getBytes();//converting string into byte array Output:
Success...
fout.write(b);
fout.close();
The content of a text file testout.txt is set with
System.out.println("success..."); the data Welcome Class.
}
catch(Exception e) testout.txt
{System.out.println(e);}
} Welcome Class
}
Java FileInputStream Class (Example read single character)
15
import java.io.FileInputStream;
public class DataStreamExample Note: Before running the code, a text file
{ named as "testout.txt" is required to be
public static void main(String args[]) throws Exception created. In this file, we are having
{ following content:
try Welcome Class
{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=fin.read();
System.out.print((char)i); Output:
fin.close(); Success...
}
catch(Exception e) testout.txt
{System.out.println(e);}
W
}
}
Java FileInputStream Class (Example read all characters)
16
import java.io.FileInputStream;
public class DataStreamExample Note: Before running the code, a text file
{ named as "testout.txt" is required to be
created. In this file, we are having following
public static void main(String args[]) throws Exception
content:
{
Welcome Class
try
{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
int i=0;
while((i=fin.read())!=-1){ Output:
System.out.print((char)i);} Success...
fin.close();
} testout.txt
catch(Exception e)
Welcome Class
{System.out.println(e);}
}
}
Character Streams
17
Character Streams
• When the I/O stream manages 16-bit Unicode characters, it is called a character
stream.
• Many classes related to character streams.
• Java FileWriter class is used to write character-oriented data to a file.
• Java FileReader class is used to read character-oriented data from the file.
Java FileWriter Class (Example write data)
18
import java.io.FileWriter; byte b[]=s.getBytes();
public class FileWriterExample You don't need to convert string into byte array because it
provides method to write string directly.
{
public static void main(String args[]) throws Exception
{
try
{
FileWriter fw=new FileWriter("D:\\testout.txt");
fw.write("Welcome Class"); Output:
fw.close(); Success...
}
catch(Exception e) testout.txt
{System.out.println(e);}
System.out.println("Success..."); Welcome Class
}
}
Java FileReader Class (Example read data)
19
import java.io.FileReader;
public class FileReaderExample
{
public static void main(String args[]) throws Exception
{
FileReader fr=new FileReader("D:\\testout.txt");
int i;
Output:
while((i=fr.read())!=-1) Success...
System.out.print((char)i);
fr.close(); testout.txt
} Welcome Class
}
Java BufferedOutputStream Class
20
• Java BufferedOutputStream class is used for buffering an output stream.
• Internally uses buffer to store data.
• Adds more efficiency than to write data directly into a stream.
• Makes the performance fast.
Java BufferedOutputStream Class (Example write data)
21
import java.io.*;
public class BufferedOutputStreamExample
{ Output:
public static void main(String args[]) throws Exception Success...
{
testout.txt
FileOutputStream fout=new FileOutputStream("D:\\testout.txt");
BufferedOutputStream bout=new BufferedOutputStream(fout); Welcome Class
String s="Welcome Class";
byte b[]=s.getBytes();//converting string into byte array
bout.write(b);
bout.flush(); In this example, we are writing the information in the
bout.close(); BufferedOutputStream object which is connected to
the FileOutputStream object. The flush() flushes the
fout.close();
data of one stream and send it into another.
System.out.println("success");
}
}
Java BufferedInputStream Class
22
• Java BufferedInputStream class is used to read information from stream.
• Internally uses buffer mechanism to make the performance fast.
The important points about BufferedInputStream are:
• When the bytes from the stream are read, the internal buffer
automatically refilled from the contained input stream, many bytes at
a time.
• When a BufferedInputStream is created, an internal buffer array is
created.
Java BufferedInputStream Class (Example read data)
23
import java.io.*;
public class BufferedInputStreamExample
{
Output:
public static void main(String args[])
Success...
{
try
testout.txt
{
FileInputStream fin=new FileInputStream("D:\\testout.txt");
Welcome Class
BufferedInputStream bin=new BufferedInputStream(fin);
int i;
while((i=bin.read())!=-1)
{System.out.print((char)i);}
bin.close(); Here, we are assuming that you have following data in
fin.close(); "testout.txt" file.
}
catch(Exception e)
{System.out.println(e);}
}
}