List of usage examples for java.io RandomAccessFile RandomAccessFile
public RandomAccessFile(File file, String mode) throws FileNotFoundException
From source file:Main.java
public static void main(String[] args) { try {//from w w w .j a va2 s .c om RandomAccessFile raf = new RandomAccessFile("c:\\temp\\RAFsample.txt", "rw"); raf.writeInt(10); raf.writeInt(43); raf.writeInt(88); raf.writeInt(455); raf.seek((3 - 1) * 4); raf.writeInt(99); raf.seek(0); int i = raf.readInt(); while (i != -1) { System.out.println(i); i = raf.readInt(); } raf.close(); } catch (IOException e) { } }
From source file:Main.java
public static void main(String[] args) { try {/* w w w. j ava 2s.c om*/ double d = 1234.5678; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeDouble(d); raf.seek(0); System.out.println(raf.readDouble()); raf.seek(0); raf.writeDouble(123.4567); raf.seek(0); System.out.println(raf.readDouble()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {// w w w . ja va 2 s .c om float f = 1234.5678f; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeFloat(f); raf.seek(0); System.out.println(raf.readFloat()); raf.seek(0); raf.writeFloat(123.4567f); raf.seek(0); 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 w w . j av a2 s . c om byte[] b1 = { 15, 8 }; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeByte(b1[0]); raf.seek(0); System.out.println(raf.readByte()); raf.seek(0); raf.writeByte(b1[1]); raf.seek(0); 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 ww .j ava 2 s . c om*/ 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:Main.java
public static void main(String[] args) { try {//from w ww . jav a 2s . 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:MainClass.java
public static void main(String[] args) throws IOException { RandomAccessFile raf = new RandomAccessFile(new File("temp.tmp"), "rw"); raf.writeInt(1);//from w ww . j a va 2 s . c o m for (int i = 0; i < 10; i++) { raf.seek(raf.length() - 4); raf.writeInt(raf.readInt()); } raf.close(); }
From source file:Main.java
public static void main(String[] args) { try {//from www. ja v a 2 s .c o 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:Main.java
public static void main(String[] args) { try {//from ww w .j a v a 2 s . c om long f = 123456789909876L; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeLong(f); raf.seek(0); System.out.println(raf.readLong()); raf.seek(0); raf.writeLong(20000000000l); raf.seek(0); System.out.println(raf.readLong()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:MainClass.java
public static void main(String[] args) { try {// w w w . j a v a2 s.com RandomAccessFile raf = new RandomAccessFile("c:\\temp\\RAFsample.txt", "rw"); raf.writeInt(10); raf.writeInt(43); raf.writeInt(88); raf.writeInt(455); // change the 3rd integer from 88 to 99 raf.seek((3 - 1) * 4); raf.writeInt(99); raf.seek(0); // go to the first integer int i = raf.readInt(); while (i != -1) { System.out.println(i); i = raf.readInt(); } raf.close(); } catch (IOException e) { } }