Example usage for java.io RandomAccessFile writeChar

List of usage examples for java.io RandomAccessFile writeChar

Introduction

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

Prototype

public final void writeChar(int v) throws IOException 

Source Link

Document

Writes a char to the file as a two-byte value, high byte first.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {//  w w  w .  j  a  v a 2 s . c  om
        int i = 70;

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

        raf.writeChar(i);

        raf.seek(0);

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

        raf.seek(0);

        raf.writeChar(71);

        raf.seek(0);

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

}

From source file:RandomIOApp.java

public static void main(String args[]) throws IOException {
    RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
    file.writeBoolean(true);/*ww  w  . j a  v  a2 s .com*/
    file.writeInt(123456);
    file.writeChar('j');
    file.writeDouble(1234.56);
    file.seek(1);
    System.out.println(file.readInt());
    System.out.println(file.readChar());
    System.out.println(file.readDouble());
    file.seek(0);
    System.out.println(file.readBoolean());
    file.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {//from w w w .j  ava 2  s .  c  om
        char c = 'J';

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

        // write something in the file
        raf.writeChar('A');

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

        // read char
        System.out.println(raf.readChar());

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

        // write a char at the start
        raf.writeChar(c);

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

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

}

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();/*from  w w w .j a  va2 s  .com*/
        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;
}