List of usage examples for java.io RandomAccessFile RandomAccessFile
public RandomAccessFile(File file, String mode) throws FileNotFoundException
From source file:ReverseFile.java
public static void main(String args[]) throws Exception { RandomAccessFile raf = new RandomAccessFile(args[0], "r"); long position = raf.length(); while (position > 0) { position -= 1;// w ww.j a v a 2 s. c o m raf.seek(position); byte b = raf.readByte(); System.out.print((char) b); } }
From source file:Main.java
public static void main(String[] args) { try {// w w w.ja v a 2 s .c om 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 ww w . ja va 2 s . c om*/ 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 {//from w w w .j a v a2s . c o m 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[] argv) throws Exception { RandomAccessFile file = new RandomAccessFile("scores.html", "rw"); for (int i = 1; i <= 6; i++) { System.out.println(file.readLine()); }/*from w w w .j a v a 2 s.c o m*/ long current = file.getFilePointer(); file.seek(current + 6); file.write("34".getBytes()); for (int i = 1; i <= 5; i++) { System.out.println(file.readLine()); } current = file.getFilePointer(); file.seek(current + 6); file.write("27".getBytes()); file.close(); }
From source file:Main.java
public static void main(String[] args) { try {//from w w w . j a va 2s . c om 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 {//from w ww. j av a 2s . co m RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeBytes("Hello World from java2s.com"); raf.seek(0); System.out.println(raf.readLine()); raf.seek(0); raf.writeBytes("This is an example from java2s.com"); raf.seek(0); System.out.println(raf.readLine()); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { RandomAccessFile file = new RandomAccessFile(new File("scores.html"), "rw"); for (int i = 1; i <= 6; i++) { System.out.println(file.readLine()); }//w w w . j av a 2 s. c o m file.close(); }
From source file:Main.java
public static void main(String[] args) { try {/* ww w.ja v a2 s .com*/ 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 {/* ww w. j a v a2 s . 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()); // 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(); } }