List of usage examples for java.io RandomAccessFile close
public void close() throws IOException
From source file:Main.java
public static void main(String[] args) { try {//from w w w. j av a2 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 {/*from w ww.ja v a 2 s . co 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 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 ww . ja v a 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 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[] args) { try {//w w w . j av 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); // 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[] argv) throws Exception { RandomAccessFile file = new RandomAccessFile("scores.html", "rw"); for (int i = 1; i <= 6; i++) { System.out.println(file.readLine()); }//from w ww. j av 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 {// ww w . j a va2 s.c o m String s = "Hello World from java2s.com"; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeChars(s); raf.seek(0); for (int i = 0; i < 15; i++) { System.out.println(raf.readChar()); } raf.seek(0); raf.writeChars("This is an example from java2s.com"); raf.seek(0); for (int i = 0; i < 20; i++) { 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. j av a 2 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()); // 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(); } }
From source file:Main.java
public static void main(String[] args) { try {/*from ww w . j av a 2s . c om*/ 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:MainClass.java
public static void main(String[] args) { try {//from ww w .j a v a 2s .c o m 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) { } }
From source file:Main.java
public static void main(String[] args) { try {/* w w w. j av a2s . c o 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("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(); } }