Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>java.io.RandomAccessFile is the class you're looking for. Here's an example implementation (you'll probably want to write some unit tests, as I haven't :)</p> <pre><code>package test; import java.io.IOException; import java.io.RandomAccessFile; public class Raf { private static class Record{ private final double price; private final int id; private final int stock; public Record(int id, int stock, double price){ this.id = id; this.stock = stock; this.price = price; } public void pack(int n, int offset, byte[] array){ array[offset + 0] = (byte)(n &amp; 0xff); array[offset + 1] = (byte)((n &gt;&gt; 8) &amp; 0xff); array[offset + 2] = (byte)((n &gt;&gt; 16) &amp; 0xff); array[offset + 3] = (byte)((n &gt;&gt; 24) &amp; 0xff); } public void pack(double n, int offset, byte[] array){ long bytes = Double.doubleToRawLongBits(n); pack((int) (bytes &amp; 0xffffffff), offset, array); pack((int) ((bytes &gt;&gt; 32) &amp; 0xffffffff), offset + 4, array); } public byte[] getBytes() { byte[] record = new byte[16]; pack(id, 0, record); pack(stock, 4, record); pack(price, 8, record); return record; } } private static final int RECORD_SIZE = 16; private static final int N_RECORDS = 1024; /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { RandomAccessFile raf = new RandomAccessFile(args[0], "rw"); try{ raf.seek(RECORD_SIZE * N_RECORDS); raf.seek(0); raf.write(new Record(1001, 476, 28.35).getBytes()); raf.write(new Record(1002, 240, 32.56).getBytes()); } finally { raf.close(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload