RandomAccessFile: read(byte[] b) : RandomAccessFile « java.io « Java by API






RandomAccessFile: read(byte[] b)

 
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();
  }
}

   
  








Related examples in the same category

1.new RandomAccessFile(String fileName, String mode)
2.RandomAccessFile: close()
3.RandomAccessFile: getChannel()
4.RandomAccessFile: getFilePointer()
5.RandomAccessFile: length()
6.RandomAccessFile: readBoolean()
7.RandomAccessFile: readByte()
8.RandomAccessFile: readChar()
9.RandomAccessFile: readDouble()
10.RandomAccessFile: readInt()
11.RandomAccessFile: readLine()
12.RandomAccessFile: seek(long pos)
13.RandomAccessFile: write(byte[] b)
14.RandomAccessFile: writeBoolean(boolean v)
15.RandomAccessFile: writeBytes(String s)
16.RandomAccessFile: writeChar(int v)
17.RandomAccessFile: writeChars(String s)
18.RandomAccessFile: writeDouble(double v)
19.RandomAccessFile: writeInt(int v)
20.RandomAccessFile: writeUTF(String str)