File Handling
File Handling
➢ Java has features that support functions for input and output with the help of
stream bytes.
➢ java.io is the package that has support to all input and output related classes.
Basics of Streams
➢ Sequence of data is called as stream.
➢ To read data from the source, the input stream is used and to write data to a
source the output stream is used.
➢ In java the inputstream and outputstream are the basis stream classes.
➢ To perform input and output of 8 bit data, in java it uses java byte streams.
1. Standard Input:
We normally use a keyboard to feed data to the program as standard input
stream which is represented in System.in
2. Standard Output:
Normally a computer screen is used as a standard output stream and is
represented as System.out.
3. Standard Error:
During the execution of a program, the error data at the output which is
produced by the user program for which the computer screen is used as
standard error stream which is represented by System.err.
Stream classes
➢ In java the package called java.io which is categorized in two parts for which one
is for handling writing and reading of bytes i.e. ByteStream and other is for
reading and writing of characters i.e. CharacterStream.
Byte Stream:
➢ For handling input and output of bytes, Byte Stream is used.
➢ There are two abstract classes defined at the top for Byte Stream which are
InputStream and OutputStream.
➢ Below here the figure shows the hierarchy of the Byte Stream.
Byte Stream
InputStream OutputStream
Character Stream
➢ To handle input and output of characters, the Character Stream is used.
➢ It uses the Unicode that can be internationalized.
➢ Below is the figure that uses the class hierarchy of character Stream.
The object of InputStream has a list of methods that works as helper and are
used to read to stream or to perform any other operations on the stream.
3 finalize() The close all the connections to the file, this method is used.
4 Available() To read and give number of bytes from the file, this is used.
➢ To create file and write data to the file, we can use the outputstream.
➢ If the file does exist then it is created before opening the file.
➢ A list of helper methods is available with the objects of OutputStream which can
be used to read the stream or perform some other operations.
Table 6.2 Methods
Sr. No Method Explanation
1 close() To shutdown the file, we use this.
To write specified number of bytes to the output stream, we
2 write()
use this.
3 finalize() To close the file and clean all the connections, we use this.
Example
import java.io.*;
public class FileDemo
{
public static void main( String[] args )
{
try {
if (file.createNewFile()){
System.out.println("File is successfully created!");
}else{
System.out.println("File already exists.");
}
file.delete();
System.out.println("file is deleted");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
File is successfully created!
File is deleted
Example:
import java.io.*;
class DemoRead
{
public static void main(String args[]){
try{
FileInputStream fin=new FileInputStream("f1.txt");
int i=0;
while((i=fin.read())!=-1){
System.out.println((char)i);
}
fin.close();
}catch(Exception e){system.out.println(e);}
}
}
Output:
Java Programming
FileOutputStream class
Example:
import java.io.*;
class Test
{
public static void main(String args[])
{
try{
FileOutputstream fout=new FileOutputStream("f1.txt");
String s="Java Programming";
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("Successfully Completed….");
}catch(Exception e){system.out.println(e);}
}
}
Output:
Successfully Completed
Video Links:
https://www.youtube.com/watch?v=ufdChySSFc0
https://www.youtube.com/watch?v=BxCbxfpwC7Q