How to Read/write from file using RandomAccessFile

RandomAccessFile allows us to read/write at random positions

By using seek method we can move to random position,
  • if seek is set beyond the length the file and we try to read from there than java.io.EOFException is thrown.
  • When we try to write at random position, data is not inserted, in fact it’s overridden.



Program to read/write from file using RandomAccessFile >
Note : Before executing below program, remove everything from c:/myFile.txt for better understanding of program
Or preferably you may delete this file because if file won’t exist than RandomAccessFile will create new file.
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileTest {
public static void main(String[] args) {
     try {
          String fileName = "c:/myFile.txt";
          String data = "abcdef";
          RandomAccessFile randomAccessFile;
         
          // ---- Writing in file using RandomAccessFile ----
          // 'rw' means opening file in Read-Write mode
          randomAccessFile = new RandomAccessFile(fileName, "rw");
          randomAccessFile.seek(5);
          randomAccessFile.writeUTF(data);
          System.out.println("Data written in "+" = "+data);
          randomAccessFile.close();
          // ---- Reading from file using RandomAccessFile ----
          // 'r' means opening file in Read mode
          randomAccessFile = new RandomAccessFile(fileName, "r");
          randomAccessFile.seek(5);
          data = randomAccessFile.readUTF();
          System.out.println("Data read from file = "+data);
          randomAccessFile.close();
     } catch (IOException e) {
          e.printStackTrace();
     }
}
}
/*
Data written in file = abcdef
Data read from file = abcdef
*/


After executing above program c:/myFile.txt will look like this >




Special characters written in file using  RandomAccessFile’s writeUTF(“String”) method?

Don't worry about special characters in file.
writeUTF(“String”) method writes a string in file using UTF-8 encoding.
First, two bytes are written to the file to identify number of bytes to follow and not the length of the string.
Following the first two bytes is each character of the string written using UTF-8 encoding.


Example -
Simply execute the following program
Note : Before executing below program, remove everything from c:/myFile.txt for better understanding of program
Or preferably you may delete this file because if file won’t exist than RandomAccessFile will create new file.

import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileTest0 {
public static void main(String[] args) throws IOException {
     // ---- Writing in file using RandomAccessFile ----
     RandomAccessFile randomAccessFile = new RandomAccessFile("c:/myFile.txt", "rw");
     randomAccessFile.writeUTF("abcdef");
     randomAccessFile.close();
}
}

After executing above program c:/myFile.txt will look like this >
We see that first two bytes are written to the file to identify number of bytes to follow and not the length of the string.



Than how to view file in Notepad without special characters using RandomAccessFile?
Rather than going for writeUTF(“String”) method which writes a string in file using UTF-8 encoding, go for  ASCII encoding for characters using the RandomAccessFile’s writeBytes and readLine methods.
No, additional information is prefixed with file using ASCII encoding (ASCII is default encoding for the notepad).

Let’s use RandomAccessFile’s writeBytes and readLine methods >
Note : Before executing below program, remove everything from c:/myFile.txt for better understanding of program
Or preferably you may delete this file because if file won’t exist than RandomAccessFile will create new file.
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileTest1 {
public static void main(String[] args) {
     try {
          String fileName = "c:/myFile.txt";
          String data = "abcdef";
          // -- Writing in file using RandomAccessFile --
          RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "rw");
          randomAccessFile.writeBytes(data);
          System.out.println("Data written in "+" = "+data);
          randomAccessFile.close();
          // -- Reading from file using RandomAccessFile --
          randomAccessFile = new RandomAccessFile(fileName, "r");
          data = randomAccessFile.readLine();
          System.out.println("Data read from file = "+data);
          randomAccessFile.close();
     } catch (IOException e) {
     e.printStackTrace();
     }
}
}
/*
Data written in file = abcdef
Data read from file = abcdef
*/

After executing above program c:/myFile.txt will look like this >
As we can see now,  No additional information is prefixed with file using ASCII encoding (ASCII is default encoding for the notepad).

No comments:

Post a Comment