Code example for RandomAccessFile

Methods: closereadFullyseekwriteBytes

0
     * java.io.RandomAccessFile#writeBytes(java.lang.String) 
     * java.io.RandomAccessFile#readFully(byte[]) 
     */ 
    public void test_readFully$B_writeBytesLjava_lang_String() throws IOException { 
        byte[] buf = new byte[testLength];
        RandomAccessFile raf = new java.io.RandomAccessFile(fileName, "rw");
        raf.writeBytes(testString);
        raf.seek(0);
 
        try { 
            raf.readFully(null);
            fail("Test 1: NullPointerException expected.");
        } catch (NullPointerException e) {
            // Expected. 
        } 
 
        raf.readFully(buf);
        assertEquals("Test 2: Incorrect bytes written or read;",
                testString, new String(buf));
 
        try { 
            raf.readFully(buf);
            fail("Test 3: EOFException expected.");
        } catch (EOFException e) {
            // Expected. 
        } 
 
        raf.close();
        try { 
            raf.writeBytes("Already closed.");
            fail("Test 4: IOException expected.");
        } catch (IOException e) {
            // Expected. 
        } 
        try { 
            raf.readFully(buf);
            fail("Test 5: IOException expected.");
        } catch (IOException e) {
            // Expected. 
        } 
    }