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 2 s . c o m*/ String s = "Hello world from java2s.com"; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write something in the file raf.writeUTF(s); // set the file pointer at 0 position raf.seek(0); // create an array equal to the length of raf byte[] arr = new byte[(int) raf.length()]; // read the file raf.readFully(arr); // create a new string based on arr String s2 = new String(arr); System.out.println(s2); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {/* w w w.j a v a2s . c om*/ 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 a v a 2 s . co m*/ String s = "Hello world from java2s.com"; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write something in the file raf.writeUTF(s); // set the file pointer at 0 position raf.seek(0); // create an array equal to the length of raf byte[] arr = new byte[10]; // read the file raf.readFully(arr, 3, 7); // create a new string based on arr String s2 = new String(arr); // print it System.out.println(s2); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] argv) throws Exception { FileChannel rwChannel = new RandomAccessFile(new File("test.txt"), "rw").getChannel(); FileChannel roChannel = new RandomAccessFile(new File("test.txt"), "r").getChannel(); FileChannel pvChannel = new RandomAccessFile(new File("text.txt"), "rw").getChannel(); ByteBuffer pvBuf = roChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int) rwChannel.size()); }
From source file:MainClass.java
public static void main(String[] argv) throws Exception { RandomAccessFile raf = new RandomAccessFile("test.txt", "r"); FileChannel fc = raf.getChannel(); MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); buffer.clear();/*from w w w . ja va 2 s . co m*/ buffer.flip(); System.out.println("hasArray=" + buffer.hasArray()); System.out.println(buffer.toString()); System.out.flush(); }
From source file:Main.java
public static void main(String[] args) { try {//from ww w . ja v a2s . c o m String s = "Hello world from java2s.com"; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); raf.writeUTF(s); raf.seek(0); System.out.println(raf.readUTF()); raf.seek(0); raf.writeUTF("This is an example from java2s.com"); raf.seek(0); System.out.println(raf.readUTF()); raf.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:InputOutputDemoPrimitive.java
public static void main(String[] a) throws Exception { //Read and write parts of file "raf.dat" in arbitrary order: RandomAccessFile raf = new RandomAccessFile("r.dat", "rw"); raf.writeDouble(3.1415);/* www . java2 s. com*/ raf.writeInt(42); raf.seek(0); System.out.println(raf.readDouble() + " " + raf.readInt()); }
From source file:MainClass.java
public static void main(String[] args) throws IOException { FileChannel fc = new RandomAccessFile("temp.tmp", "rw").getChannel(); IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer(); for (int i = 0; i < 10; i++) ib.put(i);//www.j ava 2s. c o m fc.close(); }
From source file:Main.java
public static void main(String[] args) { try {/*from w w w .jav a 2 s. co m*/ // create a new RandomAccessFile with filename Example 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()); // return the file pointer System.out.println(raf.getFilePointer()); // change the position of the file pointer raf.seek(5); // return the file pointer System.out.println(raf.getFilePointer()); // close the strea and release resources raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:MainClass.java
public static void main(String[] argv) throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile("test.dat", "r"); randomAccessFile.seek(1000);/*from w w w.j av a2 s . co m*/ FileChannel fileChannel = randomAccessFile.getChannel(); // This will print "1000" System.out.println("file pos: " + fileChannel.position()); randomAccessFile.seek(500); // This will print "500" System.out.println("file pos: " + fileChannel.position()); fileChannel.position(200); // This will print "200" System.out.println("file pos: " + randomAccessFile.getFilePointer()); }