Java RandomAccessFile(String name, String mode) Constructor
Syntax
RandomAccessFile(String name, String mode) constructor from RandomAccessFile has the following syntax.
public RandomAccessFile(String name, String mode) throws FileNotFoundException
Example
In the following code shows how to use RandomAccessFile.RandomAccessFile(String name, String mode) constructor.
import java.io.RandomAccessFile;
//from w ww .ja v a 2 s. c o m
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.
Home »
Java Tutorial »
java.io »
Java Tutorial »
java.io »