In this section, you will learn about the RandomAccess File class provided by java.io package. This is a class that allows you to read and write arbitrary bytes, text, and primitive Java data types from or to any specified location in a file. As the name suggests, random that means it is not sequential and the data can be read from and written to any specified position in the file.
Mostly, Users use the input stream or output stream in a sequential but Unlike the input and output streams java.io. RandomAccessFile is used for both reading and writing files A random access file treats with a large array of bytes stored in the file system and uses the file pointer to deal with the indexe of that array. This file pointer points the positions in the file where the reading or writing operation has to be done.
Random AccessFile implements the same interfaces as the DataInputStream and the DataOutputStream. Thus it defines the same methods for reading and writing data to a file.
This program shows the implementation of the RandomAccessFile class. Program takes an input from user to the name of a file and checks if it exists then it writes the specified string using the method writeChars( ); Otherwise it displays the appropriate message as "File does not exist". An
IOException
may be thrown if the stream has been closed.There are following methods has been used in this program:
RandomAccessFile rand = new RandomAccessFile(file,"rw");
The above code creates an instance of the RandomAccessFile class mentioning the mode in which file has to be opened. The constructor RandomAccessFile( ) takes two arguments: First is the file name and another is the operation mode (read-write mode). We are opening the file into read and write mode ("rw").
There are following methods that are used in the given program shown as.
rand.seek(file.length());
This is the seek( ) method of the RandomAccessFile class has been used to jump the file pointer at the specified. Here, file.length( ) returns the end of the file.
rand.close();
This is the close( ) method of the RandomAccessFile class has been used to close the created the instance of the RandomAccessFile class.
writeBytes( );
This the writeBytes( ) method which simply writes the content into the file. This method always append the file content with your specified text.
Here is the code of the program :
import java.io.*; public class RandAccessFile{ public static void main(String[] args) throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter File name : "); String str = in.readLine(); File file = new File(str); if(!file.exists()) { System.out.println("File does not exist."); System.exit(0); } try{ //Open the file for both reading and writing RandomAccessFile rand = new RandomAccessFile(file,"rw"); rand.seek(file.length()); //Seek to end of file rand.writeBytes("Roseindia.net,"); //Write end of file rand.close(); System.out.println("Write Successfully"); } catch(IOException e) { System.out.println(e.getMessage()); } } }Output of the Program:
C:\nisha>javac RandAccessFile.java C:\nisha>java RandAccessFile Enter File name : Filterfile.txt Write Successfully |
Download this example.
The another program reads the characters from a file using the
readByte( ) method.import java.io.*; public class ReadAccessFile{ public static void main(String[] args) throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter File name : "); String str = in.readLine(); File file = new File(str); if(!file.exists()) { System.out.println("File does not exist."); System.exit(0); } try{ //Open the file for both reading and writing RandomAccessFile rand = new RandomAccessFile(file,"r"); int i=(int)rand.length(); System.out.println("Length: " + i); rand.seek(0); //Seek to start point of file for(int ct = 0; ct < i; ct++){ byte b = rand.readByte(); //read byte from the file System.out.print((char)b); //convert byte into char } rand.close(); } catch(IOException e) { System.out.println(e.getMessage()); } } }Output of the Program:
C:\nisha>java ReadAccessFile Enter File name : Filterfile.txt Length: 30 ??t h i s i s a f i l e C:\nisha> |
No comments:
Post a Comment