RandomAccessFile(String name, String mode) constructor from RandomAccessFile has the following syntax.
public RandomAccessFile(String name, String mode) throws FileNotFoundException
In the following code shows how to use RandomAccessFile.RandomAccessFile(String name, String mode) constructor.
import java.io.RandomAccessFile; //from w w w.jav a 2 s . c om public class Main { public static void main(String[] args) throws Exception { RandomAccessFile randomAccessFile = null; String line1 = "java2s.com\n"; String line2 = "asdf1234\n"; // read / write permissions randomAccessFile = new RandomAccessFile("yourFile.dat", "rw"); randomAccessFile.writeBytes(line1); randomAccessFile.writeBytes(line2); // Place the file pointer at the end of the first line randomAccessFile.seek(line1.length()); byte[] buffer = new byte[line2.length()]; randomAccessFile.read(buffer); System.out.println(new String(buffer)); randomAccessFile.close(); } }
The code above generates the following result.