Example usage for java.io RandomAccessFile writeLong

List of usage examples for java.io RandomAccessFile writeLong

Introduction

In this page you can find the example usage for java.io RandomAccessFile writeLong.

Prototype

public final void writeLong(long v) throws IOException 

Source Link

Document

Writes a long to the file as eight bytes, high byte first.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {// w  w w . jav  a2s .  c  o m
        long f = 123456789909876L;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeLong(f);

        raf.seek(0);

        System.out.println(raf.readLong());

        raf.seek(0);

        raf.writeLong(20000000000l);

        raf.seek(0);

        System.out.println(raf.readLong());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//  ww  w  . ja  va 2  s.  c om
        long l = 12345676789098L;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        // write something in the file
        raf.writeLong(l);

        // set the file pointer at 0 position
        raf.seek(0);

        System.out.println(raf.readLong());

        // set the file pointer at 0 position
        raf.seek(0);

        // write something in the file
        raf.writeLong(12345676789099L);

        raf.seek(0);
        System.out.println(raf.readLong());
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:com.btoddb.fastpersitentqueue.JournalFileTest.java

@Test
public void testInitForReadingThenClose() throws IOException {
    UUID id = new UUID();
    RandomAccessFile raFile = new RandomAccessFile(theFile, "rw");
    raFile.writeInt(1);/*from  w w w .  java2s  .c  om*/
    Utils.writeUuidToFile(raFile, id);
    raFile.writeLong(123);
    raFile.close();

    JournalFile jf1 = new JournalFile(theFile);
    jf1.initForReading();
    assertThat(jf1.getVersion(), is(JournalFile.VERSION));
    assertThat(jf1.getId(), is(id));
    assertThat(jf1.getNumberOfEntries(), is(123L));

    assertThat(jf1.isOpen(), is(true));
    assertThat(jf1.isWriteMode(), is(false));
    assertThat(jf1.getFilePosition(), is((long) JournalFile.HEADER_SIZE));
    jf1.close();

    assertThat(jf1.isOpen(), is(false));
}

From source file:WordList.java

public static void writeWords(String filename, String[] words) throws IOException {
    // Open the file for read/write access ("rw"). We only need to write,
    // but have to request read access as well
    RandomAccessFile f = new RandomAccessFile(filename, "rw");

    // This array will hold the positions of each word in the file
    long wordPositions[] = new long[words.length];

    // Reserve space at the start of the file for the wordPositions array
    // and the length of that array. 4 bytes for length plus 8 bytes for
    // each long value in the array.
    f.seek(4L + (8 * words.length));//from  ww w. jav a  2s . c  o m

    // Now, loop through the words and write them out to the file,
    // recording the start position of each word. Note that the
    // text is written in the UTF-8 encoding, which uses 1, 2, or 3 bytes
    // per character, so we can't assume that the string length equals
    // the string size on the disk. Also note that the writeUTF() method
    // records the length of the string so it can be read by readUTF().
    for (int i = 0; i < words.length; i++) {
        wordPositions[i] = f.getFilePointer(); // record file position
        f.writeUTF(words[i]); // write word
    }

    // Now go back to the beginning of the file and write the positions
    f.seek(0L); // Start at beginning
    f.writeInt(wordPositions.length); // Write array length
    for (int i = 0; i < wordPositions.length; i++)
        // Loop through array
        f.writeLong(wordPositions[i]); // Write array element
    f.close(); // Close the file when done.
}

From source file:org.apache.activemq.store.kahadb.KahaDBStoreRecoveryBrokerTest.java

@Override
@SuppressWarnings("resource")
protected BrokerService createRestartedBroker() throws Exception {

    // corrupting index
    File index = new File(kahaDbDirectoryName + "/db.data");
    RandomAccessFile raf = new RandomAccessFile(index, "rw");
    switch (failTest) {
    case FailToLoad:
        index.delete();//  ww w . j  a va  2 s. c om
        raf = new RandomAccessFile(index, "rw");
        raf.seek(index.length());
        raf.writeBytes("corrupt");
        break;
    case LoadInvalid:
        // page size 0
        raf.seek(0);
        raf.writeBytes("corrupt and cannot load metadata");
        break;
    case LoadCorrupt:
        // loadable but invalid metadata
        // location of order index low priority index for first destination...
        raf.seek(8 * 1024 + 57);
        raf.writeLong(Integer.MAX_VALUE - 10);
        break;
    case LoadOrderIndex0:
        // loadable but invalid metadata
        // location of order index default priority index size
        // so looks like there are no ids in the order index
        // picked up by setCheckForCorruptJournalFiles
        raf.seek(12 * 1024 + 21);
        raf.writeShort(0);
        raf.writeChar(0);
        raf.writeLong(-1);
        break;
    default:
    }
    raf.close();

    // starting broker
    BrokerService broker = new BrokerService();
    KahaDBStore kaha = new KahaDBStore();
    kaha.setCheckForCorruptJournalFiles(failTest == CorruptionType.LoadOrderIndex0);
    // uncomment if you want to test archiving
    //kaha.setArchiveCorruptedIndex(true);
    kaha.setDirectory(new File(kahaDbDirectoryName));
    broker.setPersistenceAdapter(kaha);
    return broker;
}

From source file:org.apache.flume.channel.file.TestEventQueueBackingStoreFactory.java

@Test(expected = BadCheckpointException.class)
public void testCheckpointBadVersion() throws Exception {
    RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
    try {/*from  ww w.ja v  a2 s.c o m*/
        EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
        backingStore.close();
        writer.seek(EventQueueBackingStoreFile.INDEX_VERSION * Serialization.SIZE_OF_LONG);
        writer.writeLong(94L);
        writer.getFD().sync();

        backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
    } finally {
        writer.close();
    }
}

From source file:org.apache.flume.channel.file.TestEventQueueBackingStoreFactory.java

@Test(expected = BadCheckpointException.class)
public void testIncompleteCheckpoint() throws Exception {
    RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");

    try {//from   www . j ava 2s .  co m
        EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
        backingStore.close();
        writer.seek(EventQueueBackingStoreFile.INDEX_CHECKPOINT_MARKER * Serialization.SIZE_OF_LONG);
        writer.writeLong(EventQueueBackingStoreFile.CHECKPOINT_INCOMPLETE);
        writer.getFD().sync();
        backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
    } finally {
        writer.close();
    }
}

From source file:org.apache.flume.channel.file.TestEventQueueBackingStoreFactory.java

@Test(expected = BadCheckpointException.class)
public void testCheckpointVersionNotEqualToMeta() throws Exception {
    RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
    try {//from  www.j  a  v a  2 s  . co m
        EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
        backingStore.close();
        writer.seek(EventQueueBackingStoreFile.INDEX_VERSION * Serialization.SIZE_OF_LONG);
        writer.writeLong(2L);
        writer.getFD().sync();
        backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
    } finally {
        writer.close();
    }
}

From source file:org.apache.flume.channel.file.TestEventQueueBackingStoreFactory.java

@Test(expected = BadCheckpointException.class)
public void testCheckpointOrderIdNotEqualToMeta() throws Exception {
    RandomAccessFile writer = new RandomAccessFile(checkpoint, "rw");
    try {/*www  .  j a  v  a2  s  .  c o  m*/
        EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
        backingStore.close();
        writer.seek(EventQueueBackingStoreFile.INDEX_WRITE_ORDER_ID * Serialization.SIZE_OF_LONG);
        writer.writeLong(2L);
        writer.getFD().sync();
        backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
    } finally {
        writer.close();
    }
}

From source file:org.apache.flume.channel.file.TestEventQueueBackingStoreFactory.java

@Test(expected = InvalidProtocolBufferException.class)
public void testCorruptMeta() throws Throwable {
    EventQueueBackingStore backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
    backingStore.close();/*from w w w.  j  a va  2 s. co  m*/
    Assert.assertTrue(checkpoint.exists());
    File metaFile = Serialization.getMetaDataFile(checkpoint);
    Assert.assertTrue(metaFile.length() != 0);
    RandomAccessFile writer = new RandomAccessFile(metaFile, "rw");
    writer.seek(10);
    writer.writeLong(new Random().nextLong());
    writer.getFD().sync();
    writer.close();
    try {
        backingStore = EventQueueBackingStoreFactory.get(checkpoint, 10, "test");
    } catch (BadCheckpointException ex) {
        throw ex.getCause();
    }
}