Example usage for java.io RandomAccessFile writeUTF

List of usage examples for java.io RandomAccessFile writeUTF

Introduction

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

Prototype

public final void writeUTF(String str) throws IOException 

Source Link

Document

Writes a string to the file using modified UTF-8 encoding in a machine-independent manner.

Usage

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 a 2s . 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:Main.java

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

        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 va2  s.com
        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        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 channel of the file
        System.out.println(raf.getChannel());

        // 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 {// w  w w  .  j a v  a2s. com

        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) {
    try {/*w ww .j  av  a2s. com*/
        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        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 descriptor of the stream
        System.out.println(raf.getFD());

        // 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[] args) {
    try {/*from w ww  .  j  a va 2s.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 string
        System.out.println(raf.readUTF());

        // print current length
        System.out.println(raf.length());

        // set the file length to 30
        raf.setLength(30);

        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 . ja  va  2s  . c  o  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);

        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 {/*  w w  w .j a  v a  2s.  com*/
        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 {//from  www. ja va  2s  . c o 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 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();
    }

}