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 .j av a2s  .co  m*/
        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:Main.java

public static void main(String[] args) {
    try {//w w  w.ja  v a  2 s . c  om
        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 string
        System.out.println(raf.readUTF());

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

        // write something in the file
        raf.writeUTF("This is an example from java2s.com");

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

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

}

From source file:Main.java

public static void main(String[] args) {
    try {//  w  w w.  j  a  v a2 s.c o  m
        float f = 1234.56f;

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

        // write something in the file
        raf.writeFloat(987.654f);

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

        // read float
        System.out.println(raf.readFloat());

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

        // write a float
        raf.writeFloat(f);

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

        // read float
        System.out.println(raf.readFloat());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {/*from w ww .j  ava 2s.co m*/
        int i = 123;

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

        // write something in the file
        raf.writeInt(123);

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

        // print the int
        System.out.println(raf.readInt());

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

        // write something in the file
        raf.writeInt(i);

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

        // print the int
        System.out.println(raf.readInt());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:RandomFileTest.java

public static void main(String[] args) {
    Employee[] staff = new Employee[3];

    staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
    staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
    staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);

    try {/*ww  w .  j  a va2  s. com*/
        // save all employee records to the file employee.dat
        DataOutputStream out = new DataOutputStream(new FileOutputStream("employee.dat"));
        for (Employee e : staff)
            e.writeData(out);
        out.close();

        // retrieve all records into a new array
        RandomAccessFile in = new RandomAccessFile("employee.dat", "r");
        // compute the array size
        int n = (int) (in.length() / Employee.RECORD_SIZE);
        Employee[] newStaff = new Employee[n];

        // read employees in reverse order
        for (int i = n - 1; i >= 0; i--) {
            newStaff[i] = new Employee();
            in.seek(i * Employee.RECORD_SIZE);
            newStaff[i].readData(in);
        }
        in.close();

        // print the newly read employee records
        for (Employee e : newStaff)
            System.out.println(e);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from ww w . j  a v  a2 s.  co  m*/
        short s = 15000;

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

        // write something in the file
        raf.writeShort(s);

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

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

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

        // write something in the file
        raf.writeShort(134);

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

        // print the short
        System.out.println(raf.readShort());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {/*w w  w  . ja  v a 2 s  .c  o m*/

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

        // write something in the file
        raf.writeUTF("java2s.com Hello World");

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

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

        // print the length of the file
        System.out.println(raf.length());

        // write something more in the file
        raf.writeUTF("This is an example");

        // print the length of the file
        System.out.println(raf.length());
        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  .c o m*/
        // create a new RandomAccessFile with filename Example
        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        // write something in the file
        raf.writeUTF("java2s.com Hello World");

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

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

        // return the file pointer
        System.out.println(raf.getFilePointer());

        // change the position of the file pointer
        raf.seek(5);

        // return the file pointer
        System.out.println(raf.getFilePointer());

        // 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 2s. c om*/

        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 string
        System.out.println(raf.readUTF());

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

        // attempt to skip 10 bytes
        System.out.println(raf.skipBytes(10));

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

        // set the file pointer to position 8
        raf.seek(8);

        // attempt to skip 10 more bytes
        System.out.println(raf.skipBytes(10));
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

static public void main(String args[]) throws Exception {
    File infile = new File("inFilename");
    File outfile = new File("outFilename");

    RandomAccessFile inraf = new RandomAccessFile(infile, "r");
    RandomAccessFile outraf = new RandomAccessFile(outfile, "rw");

    FileChannel finc = inraf.getChannel();
    FileChannel foutc = outraf.getChannel();

    MappedByteBuffer inmbb = finc.map(FileChannel.MapMode.READ_ONLY, 0, (int) infile.length());

    Charset inCharset = Charset.forName("UTF8");
    Charset outCharset = Charset.forName("UTF16");

    CharsetDecoder inDecoder = inCharset.newDecoder();
    CharsetEncoder outEncoder = outCharset.newEncoder();

    CharBuffer cb = inDecoder.decode(inmbb);
    ByteBuffer outbb = outEncoder.encode(cb);

    foutc.write(outbb);//from   www .j a v  a  2 s .  c om

    inraf.close();
    outraf.close();
}