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 ww . j a va2 s . c om 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("java2s.com Hello World"); // set the file pointer at 0 position raf.seek(0); // read the first 8 bytes and print the number of bytes read System.out.println(raf.read(b1)); // set the file pointer at 0 position raf.seek(0); // read the first 8 bytes and print the number of bytes read System.out.println(raf.read(b2)); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { try {/* www . j a v a 2s.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(); } }
From source file:MainClass.java
public static void main(String[] args) throws IOException { FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel(); IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer(); ib.put(0);/*from w w w . j a v a2s.c o m*/ for (int i = 1; i < 10; i++) ib.put(ib.get(i - 1)); fc.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { RandomAccessFile raf = new RandomAccessFile("test.txt", "rw"); FileChannel fileChannel = raf.getChannel(); FileLock lock = null;/* w w w . ja va 2 s . c o m*/ try { lock = fileChannel.lock(0, 10, true); } catch (IOException e) { // Handle the exception } finally { if (lock != null) { try { lock.release(); } catch (IOException e) { // Handle the exception } } } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { MappedByteBuffer out = new RandomAccessFile("test.dat", "rw").getChannel() .map(FileChannel.MapMode.READ_WRITE, 0, length); for (int i = 0; i < length; i++) out.put((byte) 'x'); System.out.println("Finished writing"); for (int i = length / 2; i < length / 2 + 6; i++) System.out.print((char) out.get(i)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { File file = new File("filename"); FileChannel roChannel = new RandomAccessFile(file, "r").getChannel(); ByteBuffer roBuf = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { String filename = "test.dat"; RandomAccessFile raf1 = new RandomAccessFile(filename, "rw"); FileChannel fc1 = raf1.getChannel(); RandomAccessFile raf2 = new RandomAccessFile(filename, "rw"); FileChannel fc2 = raf2.getChannel(); System.out.println("Grabbing first lock"); FileLock lock1 = fc1.lock(0L, Integer.MAX_VALUE, false); System.out.println("Grabbing second lock"); FileLock lock2 = fc2.lock(5, 10, false); System.out.println("Exiting"); }
From source file:Main.java
public static void main(String[] argv) throws Exception { File file = new File("filename"); FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size()); buf.put(0, (byte) 0xFF); buf.force();//from ww w . jav a2s. c o m channel.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { File file = new File("filename"); FileChannel roChannel = new RandomAccessFile(file, "r").getChannel(); ByteBuffer readonlybuffer = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size()); FileChannel rwChannel = new RandomAccessFile(file, "rw").getChannel(); ByteBuffer writeonlybuffer = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int) rwChannel.size()); // Create a private (copy-on-write) memory-mapped file. FileChannel pvChannel = new RandomAccessFile(file, "rw").getChannel(); ByteBuffer privatebuffer = roChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int) rwChannel.size()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { File file = new File("filename"); FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); FileLock lock = channel.lock(); lock = channel.tryLock();/* w w w . j av a 2s .com*/ lock.release(); channel.close(); }