Example usage for java.io RandomAccessFile close

List of usage examples for java.io RandomAccessFile close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this random access file stream and releases any system resources associated with the stream.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {//  w w w .ja va  2 s  .c  om

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

        raf.writeUTF("java2s.com");

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

        // read and print the contents of the file
        System.out.println(raf.readUTF());

        // close the strea and release resources
        raf.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  w w  w.j  a va 2 s. co  m*/
        String s = "Hello world from java2s.com";

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

        raf.writeUTF(s);

        raf.seek(0);

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

        raf.seek(0);

        raf.writeUTF("This is an example from java2s.com");

        raf.seek(0);

        System.out.println(raf.readUTF());
        raf.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    RandomAccessFile raf = new RandomAccessFile("a.dat", "rw");
    int x, y;/* w  w w. j  a  v a  2  s. c o m*/

    for (long i = 0, j = raf.length() - 1; i < j; i++, j--) {
        raf.seek(i);
        x = raf.read();
        raf.seek(j);
        y = raf.read();

        raf.seek(j);
        raf.write(x);
        raf.seek(i);
        raf.write(y);

    }
    raf.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from   w w  w  .  j av  a 2 s  .c  o  m*/
        byte[] b = { 1, 2, 3, 4, 5, 6 };

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

        // write 2 bytes in the file
        raf.write(b, 2, 2);

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

        // print the two bytes we wrote
        System.out.println(raf.readByte());
        System.out.println(raf.readByte());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:MappedChannelWrite.java

public static void main(String args[]) {
    RandomAccessFile fOut;
    FileChannel fChan;/*from   w  w w.j  av  a 2  s.  com*/
    ByteBuffer mBuf;

    try {
        fOut = new RandomAccessFile("test.txt", "rw");
        fChan = fOut.getChannel();
        mBuf = fChan.map(FileChannel.MapMode.READ_WRITE, 0, 26);
        for (int i = 0; i < 26; i++)
            mBuf.put((byte) ('A' + i));

        fChan.close();
        fOut.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {/*  ww  w  .j a v  a2s.  c om*/
        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeBytes("Hello World from java2s.com");

        raf.seek(0);

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

        raf.seek(0);

        raf.writeBytes("This is an example from java2s.com");

        raf.seek(0);

        System.out.println(raf.readLine());
        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);/*from   w w  w  .ja  v  a2  s . co  m*/
    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 a  va 2 s  .  co  m*/

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");
        raf.writeUTF("Hello World from java2s.com");

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

        // print the short
        System.out.println(raf.readUnsignedShort());

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

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

}

From source file:Main.java

public static void main(String[] args) throws IOException {
    RandomAccessFile raf = new RandomAccessFile("employee.dat", "rw");

    raf.writeUTF("J");
    raf.writeUTF("S");
    raf.writeDouble(4.0);/*from   w ww .  j a  v a2s  .c  o m*/
    raf.seek(0L);
    String fname = raf.readUTF();
    String lname = raf.readUTF();
    double salary = raf.readDouble();
    System.out.println("First name = " + fname);
    System.out.println("Last name = " + lname);
    System.out.println("Salary = " + salary);
    raf.close();
}

From source file:MainClass.java

public static void main(String args[]) {
    RandomAccessFile randomAccessFile;
    FileChannel fileChannel;/* www.  j  a  v a  2 s . com*/
    ByteBuffer byteBuffer;

    try {
        randomAccessFile = new RandomAccessFile("test.txt", "rw");
        fileChannel = randomAccessFile.getChannel();
        byteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 26);
        for (int i = 0; i < 10; i++)
            byteBuffer.put((byte) ('A' + i));
        fileChannel.close();
        randomAccessFile.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}