Discuss any five classes to handle files in java.
File handling implies performing I/O on files. In Java, file I/O is performed through streams. A stream means the flow of data irrespective of the device. There are two kinds of streams: byte streams and character streams. InputStream and OutputStream are two abstract classes in Java that are provided to read/write in byte streams i.e. they perform read/write as a sequence of 8-bit bytes. FileInputStream and FileOutputStream are two concrete classes derived from InputStream and OutStream that are used to perform the actual read/write on files. Similarly, there are Reader and Writer named abstract classes in Java that are provided to read/write in character streams i.e. they perform read/write in 16-bit characters. FileReader and FileWriter are two concrete classes derived from Reader and Writer that are used to perform the actual read/write on files. Further, there is a separate class named RandomAccessFile which allows files to be read/written randomly. The classes used for file handling in Java can be pictured as:
Some classes to handle files in Java are discussed below:
(i) FileInputStream:
The FileInputStream class creates an InputStream that we can use to read bytes from a file. Its two most common constructors are shown here:
FileInputStream(String filepath) throws
FileNotFoundException FileInputStream(File fileObj) throws FileNotFoundException Here, filepath is the full path name of a file, and fileObj is a File object that describes the file.
Code example:
import java.io.*;
public class FileInputStreamTest {
public static void main(String args[]) throws IOException {
FileInputStream fin = new FileInputStream("welcome.txt");
int i = 0;
while ((i = fin.read()) != -1) {
System.out.print((char)i);
}
}
}
(ii) FileOutputStream:
FileOutputStream creates an OutputStream that we can use to write bytes to a file. Its most commonly used constructors are:
public FileOutputStream(String name) throws
FileNotFoundException public FileOutputStream(String name, boolean append) throws FileNotFoundException public FileOutputStream(File file) throws FileNotFoundException publicFileOutputStream(File file, boolean append) throws FileNotFoundException
Here, filePath is the full path name of a file, and fileObj is a File object that describes the file. If append is true, the file is opened in append mode.
Code example:
import java.io.*;
class FileOutputStreamDemo
{
public static void main(String args[])
{
try {
FileOutputStream fout = new FileOutputStream("hello.txt");
String s = "Hello World!!!";
byte b[]=s.getBytes();
fout.write(b);
}
catch(IOException e)
{
System.out.println(e);
}
}
}
(iii) FileReader:
The FileReader class creates a Reader that you can use to read the contents of a file. Its two most commonly used constructors are: public FileReader(String fileName) throws FileNotFoundException public FileReader(File file) throws FileNotFoundException Here, filePath is the full path name of a file, and fileObj is a File object that describes the file.
Code example:
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("test.txt");
int c;
while ((c = fr.read()) != -1) {
System.out.print((char) c);
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
(iv) FileWriter:
FileWriter creates a Writer that you can use to write to a file. Its most commonly used constructors are:
public FileWriter(String fileName) throws IOException
public FileWriter(String fileName, boolean append) throws IOException
public FileWriter(File file) throws IOException
public FileWriter(File file, boolean append) throws IOException
Here, filePath is the full path name of a file, and fileObj is a File object that describes the file. If append is true, then output is appended to the end of the file.
Code example:
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterDemo {
public static void main(String args[]) {
String src = "Welcome to Java World!!!";
char buffer[] = src.toCharArray();
try {
FileWriter f0 = new FileWriter("file0.txt");
FileWriter f1 = new FileWriter("file1.txt");
for (int i = 0; i < buffer.length; i = i + 2) {
f0.write(buffer[i]);
}
f1.write(buffer);
f0.close();
f1.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
This program creates two files named file0.txt and file1.txt. The first file contains every alternate characters of the buffer array and the second file contains the whole buffer.
(v) RandomAccessFile:
RandomAccessFile encapsulates a random-access file. It is not derived from InputStream or OutputStream. Instead, it implements the interfaces DataInput and DataOutput, which defines the basic I/O methods. It also supports positioning requests i.e., we can position the file pointer within the file. It has these two constructors:
RandomAccessFile(File fileObj, String access) throws FileNotFoundException
RandomAccessFile(String filename, String access) throws FileNotFoundException
In the first form, fileObj specifies the name of the file to open as a File object. In the second form, the name of the file is passed in filename. In both cases, access determines what type of file access is permitted. If it is “r”, then the file can be read, but not written. If it is “rw”, then the file is opened in read-write mode. The most important method of this class is seek( ). This method is used to set the current position of the file pointer within the file: void seek(long newPos) throws IOException Here, newPos specifies the new position, in bytes, of the file pointer from the beginning of the file. After a call to seek( ), the next read or write operation will occur at the new file position.
Code example:
import java.io.IOException;
import java.io.RandomAccessFile;
public class TestRandomAccessFile {
public static void main(String []args)
{
try
{
RandomAccessFile r = new RandomAccessFile("abc.dat", "rw");
r.writeChar('a'); //2 bytes
r.writeInt(555); //4 bytes
r.writeDouble(3.14); // 8 bytes
String s = "Random";
r.writeBytes(s); //6 bytes
r.seek(0);
System.out.println(r.readChar());
System.out.println(r.readInt());
System.out.println(r.readDouble());
r.seek(2);
System.out.println(r.readInt());
r.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Comments
Post a Comment