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 .ja va 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[] argv) throws Exception { RandomAccessFile raf = new RandomAccessFile("a.dat", "rw"); int x, y;// w w w .j av a 2 s. c o m for (long i = 0, j = raf.length() - 1; i < j; i++, j--) { raf.seek(i); x = raf.read(); raf.seek(j); y = raf.read(); raf.seek(j); raf.write(x); raf.seek(i); raf.write(y); } raf.close(); }
From source file:Main.java
public static void main(String[] args) { try {// w w w .j a v a2s . 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 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 w ww .ja v a 2s . 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 {//from w ww . j a v a 2 s . 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); 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[]) throws Exception { RandomAccessFile fh1 = new RandomAccessFile("a.txt", "r"); RandomAccessFile fh2 = new RandomAccessFile("b.txt", "r"); long filesize1 = fh1.length(); long filesize2 = fh2.length(); // allocate two buffers large enough to hold entire files int bufsize = (int) Math.min(filesize1, filesize2); byte[] buffer1 = new byte[bufsize]; byte[] buffer2 = new byte[bufsize]; fh1.readFully(buffer1, 0, bufsize);// w w w .ja v a 2s . c o m fh2.readFully(buffer2, 0, bufsize); for (int i = 0; i < bufsize; i++) { if (buffer1[i] != buffer2[i]) { System.out.println("Files differ at offset " + i); break; } } fh1.close(); fh2.close(); }
From source file:Main.java
public static void main(String[] args) { try {//from w ww .jav 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 {//from w w w . j a va2 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 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 {//from w w w . j a v a 2 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) throws Exception { RandomAccessFile raf = new RandomAccessFile("books.dat", "rw"); String books[] = new String[5]; books[0] = "A"; books[1] = "B"; books[2] = "C"; books[3] = "D"; books[4] = "E"; for (int i = 0; i < books.length; i++) { raf.writeUTF(books[i]);//from w w w . j a va 2 s. c o m } raf.seek(raf.length()); raf.writeUTF("Servlet & JSP Programming"); raf.seek(0); while (raf.getFilePointer() < raf.length()) { System.out.println(raf.readUTF()); } }