List of usage examples for java.io RandomAccessFile seek
public void seek(long pos) throws IOException
From source file:Main.java
public static void main(String[] args) { try {/* w ww . j ava2 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 {// w ww . ja v a 2s. c om 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:Main.java
public static void main(String[] args) { try {/*from ww w . ja v a 2s . c om*/ 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 .j av a 2 s . co 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 {/* w ww . jav 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 the first byte and print it System.out.println(raf.read()); // set the file pointer at 4rth position raf.seek(4); // read the first byte and print it System.out.println(raf.read()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {// w w w. j av a2s . 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); // print the byte System.out.println(raf.readUnsignedByte()); // set the file pointer at 7 position raf.seek(7); System.out.println(raf.readUnsignedByte()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {//from w ww.j a v a 2 s . co 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(); } }
From source file:Main.java
public static void main(String[] args) { try {/* ww w. j a v a 2s.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 {/*www . j ava 2 s. com*/ 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("Hello World from java2s.com"); // set the file pointer at 0 position raf.seek(0); // read 2 bytes, starting from 1 System.out.println(raf.read(b1, 1, 2)); // set the file pointer at 0 position raf.seek(0); // read 3 bytes, starting from 4rth System.out.println(raf.read(b2, 4, 3)); 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 2 s. com // 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(); } }