RandomAccessFile: writeBytes(String s)
import java.io.RandomAccessFile;
public class Main {
public static void main(String[] args) throws Exception {
RandomAccessFile randomAccessFile = null;
String line1 = "line\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();
}
}
Home
Java Book
File Stream
Java Book
File Stream
RandomAccessFile:
- RandomAccessFile
- new RandomAccessFile(String fileName, String mode)
- RandomAccessFile: close()
- RandomAccessFile: getChannel()
- RandomAccessFile: getFilePointer()
- RandomAccessFile: length()
- RandomAccessFile: read(byte[] b)
- RandomAccessFile: readBoolean()
- RandomAccessFile: readByte()
- RandomAccessFile: readChar()
- RandomAccessFile: readDouble()
- RandomAccessFile: readInt()
- RandomAccessFile: readLine()
- RandomAccessFile: seek(long pos)
- RandomAccessFile: write(byte[] b)
- RandomAccessFile: writeBoolean(boolean v)
- RandomAccessFile: writeBytes(String s)
- RandomAccessFile: writeChars(String s)
- RandomAccessFile: writeInt(int v)
- RandomAccessFile: writeUTF(String str)