Code example for RandomAccessFile

Methods: close, readUnsignedShort, seek, writeShort

0
 
    /** 
     * java.io.RandomAccessFile#readUnsignedShort() 
     */ 
    public void test_readUnsignedShort() throws IOException { 
        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
        raf.writeShort(-1);
        raf.seek(0);
 
        assertEquals("Test 1: Incorrect value written or read;",
                65535, raf.readUnsignedShort());
 
        try { 
            raf.readUnsignedShort();
            fail("Test 2: EOFException expected.");
        } catch (EOFException e) {
            // Expected. 
        } 
 
        raf.close();
        try { 
            raf.readUnsignedShort();
            fail("Test 3: IOException expected.");
        } catch (IOException e) {
            // Expected. 
        } 
    }