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 {//from   ww w  .  j a v a2  s .co  m
        String s = "Hello world from java2s.com";

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

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

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

        // create an array equal to the length of raf
        byte[] arr = new byte[(int) raf.length()];

        // read the file
        raf.readFully(arr);

        // create a new string based on arr
        String s2 = new String(arr);

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

}

From source file:Main.java

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

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

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

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

        // read byte
        System.out.println(raf.readByte());

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

        // write 0 at the start
        raf.write(0);

        // read byte
        System.out.println(raf.readByte());
        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);

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

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

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

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

        // print the string
        System.out.println(raf.readUTF());
        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 v a2s .com*/
        String s = "Hello world from java2s.com";

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

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

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

        // create an array equal to the length of raf
        byte[] arr = new byte[10];

        // read the file
        raf.readFully(arr, 3, 7);

        // create a new string based on arr
        String s2 = new String(arr);

        // print it
        System.out.println(s2);
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    RandomAccessFile randomAccessFile = null;

    String line1 = "line\n";
    String line2 = "asdf1234\n";

    // read / write permissions
    randomAccessFile = new RandomAccessFile("yourFile.dat", "rw");

    randomAccessFile.writeBytes(line1);//from   w  w  w .  j  av a2s. com
    randomAccessFile.writeBytes(line2);

    // Place the file pointer at the end of the first line
    randomAccessFile.seek(line1.length());

    byte[] buffer = new byte[line2.length()];
    randomAccessFile.read(buffer);
    System.out.println(new String(buffer));

    randomAccessFile.close();
}

From source file:Main.java

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

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

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

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

        // read boolean
        System.out.println(raf.readBoolean());

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

        // write 0 at the start
        raf.write(0);

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

}

From source file:Main.java

public static void main(String[] args) {
    try {//  w  w  w  .  ja v a2  s. com
        double d = 1.2345678;

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

        raf.writeDouble(123.456789);

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

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

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

        // write a double at the start
        raf.writeDouble(d);

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

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

}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    RandomAccessFile randomAccessFile = null;

    String line1 = "java2s.com\n";
    String line2 = "asdf1234\n";

    // read / write permissions
    randomAccessFile = new RandomAccessFile("yourFile.dat", "rw");

    randomAccessFile.writeBytes(line1);//from  w  ww.java 2s . co m
    randomAccessFile.writeBytes(line2);

    // Place the file pointer at the end of the first line
    randomAccessFile.seek(line1.length());

    byte[] buffer = new byte[line2.length()];
    randomAccessFile.read(buffer);
    System.out.println(new String(buffer));

    randomAccessFile.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {//from  w w  w  .ja  v  a2 s. c o m
        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

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

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

        // print the line
        System.out.println(raf.readLine());

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

        raf.writeUTF("This is an example \n Hello World");

        raf.seek(0);
        // print the line
        System.out.println(raf.readLine());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//ww  w  .j  a va2s  . c o m
        byte[] b1 = { 1, 2, 3 };
        byte[] b2 = { 1, 2, 3, 4, 5, 6, 7, 8 };

        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 the first 8 bytes and print the number of bytes read
        System.out.println(raf.read(b1));

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

        // read the first 8 bytes and print the number of bytes read
        System.out.println(raf.read(b2));
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}