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

Popular posts from this blog

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?

Suppose that a data warehouse consists of the four dimensions; date, spectator, location, and game, and the two measures, count and charge, where charge is the fee that a spectator pays when watching a game on a given date. Spectators may be students, adults, or seniors, with each category having its own charge rate. a) Draw a star schema diagram for the data b) Starting with the base cuboid [date; spectator; location; game], what specific OLAP operations should perform in order to list the total charge paid by student spectators at GM Place in 2004?

Explain network topology .Explain tis types with its advantages and disadvantges.