File Handleing
File Handleing
File(class file)
public class File extends Object
The java.io.file package defines interfaces and classes for the Java virtual machine to access files, file
attributes, and file systems. This API may be used to overcome many of the limitations of
the java.io.File class. The toPath method may be used to obtain a Path that uses the abstract path
represented by a File object to locate a file. The resulting Path may be used with the Files class to
provide more efficient and extensive access to additional file operations, file attributes, and I/O
exceptions to help diagnose errors when an operation on a file fails.
Before understanding the File operations, it is required that we should have knowledge
of Stream and File methods. If you have knowledge about both of them, you can skip it.
Stream:
A series of data is referred to as a stream. In Java, Stream is classified into two types, i.e., Byte
Stream and Character Stream.
Streams in Java
● In Java, a sequence of data is known as a
stream.
● This concept is used to perform I/O
operations on a file.
● There are two types of streams :
Byte Stream
Byte Stream is mainly involved with byte
data. A file handling process with a byte
stream is a process in which an input is
provided and executed with the byte data.
● Byte Input Stream: Used to read byte data from different devices.
● Byte Output Stream: Used to write byte data to different devices.
Character Stream
Character Stream is mainly involved with character data. A file handling process with a
character stream is a process in which an input is provided and executed with the character data.
This stream is used to read or write character data. Character stream is again subdivided into 2 types
which are as follows:
● Character Input Stream: Used to read character data from different devices.
● Character Output Stream: Used to write character data to different devices.
They process the data byte by byte. They process the data character by character.
They read/write data 8 bits maximum at a time. They read/write data 16 bits maximum at a time.
They are most suitable to process binary files. They are most suitable to process text files.
All byte stream classes in Java are descendants of All character stream classes in Java are
InputStream and OutputStream. descendants of Reader and Writer.
int read() This method returns an integer, an integral representation of the next
available byte of the input. The integer -1 is returned once the end of the
input is encountered.
int read (byte buffer []) This method is used to read the specified buffer length bytes from the
input and returns the total number of bytes successfully read. It returns -1
once the end of the input is encountered.
int read (byte buffer [], This method is used to read the 'nBytes' bytes from the buffer starting at
int loc, int nBytes) a specified location, 'loc'. It returns the total number of bytes successfully
read from the input. It returns -1 once the end of the input is encountered.
int available () This method returns the number of bytes that are available to read.
Void mark(int nBytes) This method is used to mark the current position in the input stream until
the specified nBytes are read.
void reset () This method is used to reset the input pointer to the previously set mark.
long skip (long nBytes) This method is used to skip the nBytes of the input stream and returns
the total number of bytes that are skipped.
void close () This method is used to close the input source. If an attempt is made to
read even after the closing, IOException is thrown by the method.
Method Description
write (byte buffer []) This method is used to read the specified buffer length bytes from the
input and returns the total number of bytes successfully read. It returns -1
once the end of the input is encountered.
flash() forces to write all data present in output stream to the destination
void close () This method is used to close the input source. If an attempt is made to
read even after the closing, IOException is thrown by the method.
Constructor Description
Reader(Object lock) Creates a new character-stream reader whose critical sections will
synchronize on the given object.
Methods Description
close() Closes the stream and releases any system resources associated with
it.
read(CharBuffer target) Attempts to read characters into the specified character buffer.
Constructor Description
Writer(Object lock) Creates a new character-stream writer whose critical sections will
synchronize on the given object.
Methods Description
Constructor Summary
File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname.
File(URI uri)
Creates a new File instance by converting the given file: URI into an abstract pathname.
Fil
e
W
rit
e
FileWriter Class Example:
import java.io.FileWriter;
import java.io.IOException;
public class file_writer{
public static void main(String[] args) {
//FileWriter fw=new FileWriter("test.txt");
try{
//if file not exist then FileWriter() create on in drive
FileWriter fw=new FileWriter("test.txt");
fw.write("Amit "); //write as string
fw.write('F'); //write single character
fw.write('I'); //write character on buffered memory
fw.write('E'); //so that it will not reflect at once
fw.write('M'); //flush() method used to store data
buffer to in a specified file
fw.flush(); //so we need to use flush() method
fw.write(" SONARPUR");
fw.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
Output: Create a test.txt file on current path and written on that file
BufferedWriter Class: Java BufferedWriter class is used to provide buffering for Writer
instances. It makes the performance fast. It inherits Writer class. The buffering characters are
used for providing the efficient writing of single arrays, characters, and strings.
Example:
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;
class buffere_writer{
public static void main(String[] args) {
Output: Create a testout.txt file on current path and written on that file
Welcome to java.
Success
Programming running...
Java 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.
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
Output: Create a testPrintWrite.txt file on current path and written on that file Java
provides of all technology.
File Read
Java
FileReader Class
Java FileReader class is used to read data from the file. It returns data in byte format like
FileInputStream class. It is character-oriented class which is used for file handling in java.
FileReader in Java is a class in the java.io package which can be used to read a stream of
characters from the files. Java IO FileReader class uses either specified charset or the
platform’s default charset for decoding from bytes to characters.
Constructor Description
FileReader(String file) It gets filename in string. It opens the given file in read mode. If file
doesn't exist, it throws FileNotFoundException.
FileReader(File file) It gets filename in file instance. It opens the given file in read mode.
If file doesn't exist, it throws FileNotFoundException.
Method Description
int read() It is used to return a character in ASCII form. It returns -1 at the end of file.
Constructor Description
BufferedReader(Reader rd) It is used to create a buffered character input stream that uses
the default size for an input buffer.
BufferedReader(Reader rd, It is used to create a buffered character input stream that uses
int size) the specified size for an input buffer.
Method Description
int read(char[] cbuf, int off, It is used for reading characters into a portion of an array.
int len)
boolean markSupported() It is used to test the input stream support for the mark and reset
method.
void reset() It repositions the stream at a position the mark method was last
called on this input stream.
void close() It closes the input stream and releases any of the system
resources associated with the stream.