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 v a 2s . com byte[] b = { 1 }; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.write(b); // set the file pointer at 0 position 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 {// ww w . j a v a2 s. 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(); } }
From source file:Tail.java
public static void main(String args[]) throws Exception { RandomAccessFile raf = new RandomAccessFile(args[0], "r"); long count = 10; long position = raf.length(); position -= count;/* www .j a va 2 s .co m*/ if (position < 0) position = 0; raf.seek(position); while (true) { try { byte b = raf.readByte(); System.out.print((char) b); } catch (EOFException eofe) { break; } } }
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 . java2s.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 {//w w w .ja v a2 s .c om boolean b = true; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeBoolean(false); raf.seek(0); System.out.println(raf.readBoolean()); raf.writeBoolean(b); raf.seek(1); System.out.println(raf.readBoolean()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {/*from w ww .jav a2s . c o m*/ int i = 70; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeChar(i); raf.seek(0); System.out.println(raf.readChar()); raf.seek(0); raf.writeChar(71); 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 {/*from ww w .ja va2s.c o m*/ short s = 15; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeShort(s); raf.seek(0); System.out.println(raf.readShort()); raf.seek(0); raf.writeShort(20); raf.seek(0); System.out.println(raf.readShort()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:MainClass.java
public static void main(String[] args) throws IOException { RandomAccessFile rf = new RandomAccessFile("test.dat", "rw"); for (int i = 0; i < 10; i++) rf.writeDouble(i * 1.414);/*from w ww . j a v a2 s .c o m*/ rf.close(); rf = new RandomAccessFile("test.dat", "rw"); rf.seek(5 * 8); rf.writeDouble(47.0001); rf.close(); rf = new RandomAccessFile("test.dat", "r"); for (int i = 0; i < 10; i++) System.out.println("Value " + i + ": " + rf.readDouble()); rf.close(); }
From source file:Main.java
public static void main(String[] args) { try {/*from w w w. j a va 2 s . com*/ int f = 1234; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeInt(f); raf.seek(0); System.out.println(raf.readInt()); raf.seek(0); raf.writeInt(200); raf.seek(0); System.out.println(raf.readInt()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:RandomIOApp.java
public static void main(String args[]) throws IOException { RandomAccessFile file = new RandomAccessFile("test.txt", "rw"); file.writeBoolean(true);/* ww w.j av a 2s . c o m*/ file.writeInt(123456); file.writeChar('j'); file.writeDouble(1234.56); file.seek(1); System.out.println(file.readInt()); System.out.println(file.readChar()); System.out.println(file.readDouble()); file.seek(0); System.out.println(file.readBoolean()); file.close(); }