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[] argv) throws Exception { // Create a read/writeable file channel File file = new File("filename"); FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); InputStream is = Channels.newInputStream(channel); // Close the channel is.close();/*from www. j a v a 2 s.co m*/ }
From source file:MainClass.java
public static void main(String args[]) { RandomAccessFile randomAccessFile; FileChannel fileChannel;/* ww w.ja v a 2s .c om*/ ByteBuffer byteBuffer; try { randomAccessFile = new RandomAccessFile("test.txt", "rw"); fileChannel = randomAccessFile.getChannel(); byteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 26); for (int i = 0; i < 10; i++) byteBuffer.put((byte) ('A' + i)); fileChannel.close(); randomAccessFile.close(); } catch (IOException exc) { System.out.println(exc); System.exit(1); } }
From source file:RawDataFromImageFilePDF.java
public static void main(String[] args) { Document document = new Document(); try {//w w w . j av a2 s . c o m PdfWriter.getInstance(document, new FileOutputStream("RawDataFromImageFilePDF.pdf")); document.open(); RandomAccessFile rf = new RandomAccessFile("logo.png", "r"); int size = (int) rf.length(); byte imext[] = new byte[size]; rf.readFully(imext); rf.close(); Image img1 = Image.getInstance(imext); img1.setAbsolutePosition(50, 500); document.add(img1); } catch (Exception e) { System.err.println(e.getMessage()); } document.close(); }
From source file:RawDataFromImageFileAndManipulationPDF.java
public static void main(String[] args) { Document document = new Document(); try {//from w w w .j a v a 2 s . com PdfWriter.getInstance(document, new FileOutputStream("RawDataFromImageFileAndManipulationPDF.pdf")); document.open(); RandomAccessFile rf = new RandomAccessFile("logo.png", "r"); int size = (int) rf.length(); byte imext[] = new byte[size]; rf.readFully(imext); rf.close(); for (int i = 0; i < imext.length; i++) { imext[i] = (byte) (imext[i] + 3); } Image img1 = Image.getInstance(100, 100, 3, 8, imext); img1.setAbsolutePosition(200, 200); document.add(img1); } catch (Exception e) { System.err.println(e.getMessage()); } document.close(); }
From source file:Main.java
static public void main(String args[]) throws Exception { File infile = new File("inFilename"); File outfile = new File("outFilename"); RandomAccessFile inraf = new RandomAccessFile(infile, "r"); RandomAccessFile outraf = new RandomAccessFile(outfile, "rw"); FileChannel finc = inraf.getChannel(); FileChannel foutc = outraf.getChannel(); MappedByteBuffer inmbb = finc.map(FileChannel.MapMode.READ_ONLY, 0, (int) infile.length()); Charset inCharset = Charset.forName("UTF8"); Charset outCharset = Charset.forName("UTF16"); CharsetDecoder inDecoder = inCharset.newDecoder(); CharsetEncoder outEncoder = outCharset.newEncoder(); CharBuffer cb = inDecoder.decode(inmbb); ByteBuffer outbb = outEncoder.encode(cb); foutc.write(outbb);/*from w w w. jav a 2 s. co m*/ inraf.close(); outraf.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { fc = new RandomAccessFile("test.dat", "rw").getChannel(); MappedByteBuffer out = fc.map(FileChannel.MapMode.READ_WRITE, 0, LENGTH); for (int i = 0; i < LENGTH; i++) out.put((byte) 'x'); new LockAndModify(out, 0, 0 + LENGTH / 3); new LockAndModify(out, LENGTH / 2, LENGTH / 2 + LENGTH / 4); }
From source file:MainClass.java
public static void main(String[] args) throws IOException { if (args.length < 2) { System.err.println("Usage: java MainClass inFile1 inFile2... outFile"); return;/*www . j a v a 2 s. c om*/ } ByteBuffer[] buffers = new ByteBuffer[args.length - 1]; for (int i = 0; i < args.length - 1; i++) { RandomAccessFile raf = new RandomAccessFile(args[i], "r"); FileChannel channel = raf.getChannel(); buffers[i] = channel.map(FileChannel.MapMode.READ_ONLY, 0, raf.length()); } FileOutputStream outFile = new FileOutputStream(args[args.length - 1]); FileChannel out = outFile.getChannel(); out.write(buffers); out.close(); }
From source file:CreateEmployeeFile.java
public static void main(String[] args) throws Exception { String[] fnames = { "A", "B", "C" }; String[] lnames = { "a", "b", "c" }; String[] addresses = { "Box 100", "55 Street", "6 Lane" }; byte[] ages = { 46, 59, 32 }; double[] salaries = { 5.0, 6.0, 3.0 }; RandomAccessFile raf = new RandomAccessFile("employee.dat", "rw"); EmployeeRecord er = new EmployeeRecord(); for (int i = 0; i < fnames.length; i++) { er.setFirstName(fnames[i]);//from www . ja v a 2 s . c o m er.setLastName(lnames[i]); er.setAddress(addresses[i]); er.setAge(ages[i]); er.setSalary(salaries[i]); er.write(raf); } raf = new RandomAccessFile("employee.dat", "rw"); er = new EmployeeRecord(); int numRecords = (int) raf.length() / er.size(); for (int i = 0; i < numRecords; i++) { er.read(raf); System.out.print(er.getFirstName() + " "); System.out.print(er.getLastName() + " "); System.out.print(er.getAddress() + " "); System.out.print(er.getAge() + " "); System.out.println(er.getSalary()); } raf.seek(0); for (int i = 0; i < numRecords; i++) { er.read(raf); if (er.getAge() >= 55) { er.setSalary(0.0); raf.seek(raf.getFilePointer() - er.size()); er.write(raf); raf.seek(raf.getFilePointer() - er.size()); er.read(raf); } System.out.print(er.getFirstName() + " "); System.out.print(er.getLastName() + " "); System.out.print(er.getAddress() + " "); System.out.print(er.getAge() + " "); System.out.println(er.getSalary()); } }
From source file:GetChannel.java
public static void main(String[] args) throws Exception { // Write a file: FileChannel fc = new FileOutputStream("data.txt").getChannel(); fc.write(ByteBuffer.wrap("Some text ".getBytes())); fc.close();/*from www . j av a2 s . c o m*/ // Add to the end of the file: fc = new RandomAccessFile("data.txt", "rw").getChannel(); fc.position(fc.size()); // Move to the end fc.write(ByteBuffer.wrap("Some more".getBytes())); fc.close(); // Read the file: fc = new FileInputStream("data.txt").getChannel(); ByteBuffer buff = ByteBuffer.allocate(BSIZE); fc.read(buff); buff.flip(); while (buff.hasRemaining()) System.out.print((char) buff.get()); }
From source file:Main.java
public static void main(String[] args) throws Exception { ZipInputStream zipinputstream = new ZipInputStream(new FileInputStream("filename")); ZipEntry zipentry = zipinputstream.getNextEntry(); while (zipentry != null) { String entryName = zipentry.getName(); File newFile = new File(entryName); String directory = newFile.getParent(); if (directory == null) { if (newFile.isDirectory()) break; }/*from w ww . j a v a2s . com*/ RandomAccessFile rf = new RandomAccessFile(entryName, "r"); String line; if ((line = rf.readLine()) != null) { System.out.println(line); } rf.close(); zipinputstream.closeEntry(); zipentry = zipinputstream.getNextEntry(); } zipinputstream.close(); }