List of usage examples for java.nio.channels FileChannel size
public abstract long size() throws IOException;
From source file:MainClass.java
public static void main(String args[]) { FileInputStream fileInputStream; FileChannel fileChannel; long fileSize; MappedByteBuffer mBuf;//from www . j a v a2 s . c o m try { fileInputStream = new FileInputStream("test.txt"); fileChannel = fileInputStream.getChannel(); fileSize = fileChannel.size(); mBuf = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize); for (int i = 0; i < fileSize; i++) System.out.print((char) mBuf.get()); fileChannel.close(); fileInputStream.close(); } catch (IOException exc) { System.out.println(exc); System.exit(1); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { File aFile = new File("C:/test.bin"); RandomAccessFile ioFile = new RandomAccessFile(aFile, " rw"); FileChannel ioChannel = ioFile.getChannel(); final int PRIMESREQUIRED = 10; long[] primes = new long[PRIMESREQUIRED]; int index = 0; final long REPLACEMENT = 999999L; final int PRIMECOUNT = (int) ioChannel.size() / 8; MappedByteBuffer buf = ioChannel.map(FileChannel.MapMode.READ_WRITE, 0L, ioChannel.size()).load(); ioChannel.close();//from w ww .ja va 2s . c o m for (int i = 0; i < PRIMESREQUIRED; i++) { index = 8 * (int) (PRIMECOUNT * Math.random()); primes[i] = buf.getLong(index); buf.putLong(index, REPLACEMENT); } for (long prime : primes) { System.out.printf("%12d", prime); } ioFile.close(); }
From source file:NIOCopy.java
public static void main(String args[]) throws Exception { FileInputStream fIn;// w w w. j a va 2s. com FileOutputStream fOut; FileChannel fIChan, fOChan; long fSize; MappedByteBuffer mBuf; fIn = new FileInputStream(args[0]); fOut = new FileOutputStream(args[1]); fIChan = fIn.getChannel(); fOChan = fOut.getChannel(); fSize = fIChan.size(); mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize); fOChan.write(mBuf); // this copies the file fIChan.close(); fIn.close(); fOChan.close(); fOut.close(); }
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();/* w w w . j a v a2 s.c om*/ // 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:MainClass.java
public static void main(String args[]) { FileInputStream fIn;// w ww .j a v a2 s . co m FileOutputStream fOut; FileChannel fIChan, fOChan; long fSize; MappedByteBuffer mBuf; try { fIn = new FileInputStream(args[0]); fOut = new FileOutputStream(args[1]); fIChan = fIn.getChannel(); fOChan = fOut.getChannel(); fSize = fIChan.size(); mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize); fOChan.write(mBuf); // this copies the file fIChan.close(); fIn.close(); fOChan.close(); fOut.close(); } catch (IOException exc) { System.out.println(exc); System.exit(1); } catch (ArrayIndexOutOfBoundsException exc) { System.out.println("Usage: Copy from to"); System.exit(1); } }
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:Copy.java
public static void main(String[] args) { FileChannel in = null; FileChannel out = null;/* w w w.j av a2s . co m*/ if (args.length < 2) { System.out.println("Usage: java Copy <from> <to>"); System.exit(1); } try { in = new FileInputStream(args[0]).getChannel(); out = new FileOutputStream(args[1]).getChannel(); out.transferFrom(in, 0L, (int) in.size()); } catch (Exception e) { e.printStackTrace(); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { createFile();/*from www . ja v a 2 s. c o m*/ File aFile = new File("C:/primes.bin"); FileInputStream inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); final int PRIMESREQUIRED = 10; ByteBuffer buf = ByteBuffer.allocate(8 * PRIMESREQUIRED); long[] primes = new long[PRIMESREQUIRED]; int index = 0; // Position for a prime in the file // Count of primes in the file final int PRIMECOUNT = (int) inChannel.size() / 8; // Read the number of random primes required for (int i = 0; i < PRIMESREQUIRED; i++) { index = 8 * (int) (PRIMECOUNT * Math.random()); inChannel.read(buf, index); // Read the value buf.flip(); primes[i] = buf.getLong(); // Save it in the array buf.clear(); } for (long prime : primes) { System.out.printf("%12d", prime); } inFile.close(); // Close the file and the channel }
From source file:BGrep.java
public static void main(String[] args) { String encodingName = "UTF-8"; // Default to UTF-8 encoding int flags = Pattern.MULTILINE; // Default regexp flags try { // Fatal exceptions are handled after this try block // First, process any options int nextarg = 0; while (args[nextarg].charAt(0) == '-') { String option = args[nextarg++]; if (option.equals("-e")) { encodingName = args[nextarg++]; } else if (option.equals("-i")) { // case-insensitive matching flags |= Pattern.CASE_INSENSITIVE; } else if (option.equals("-s")) { // Strict Unicode processing flags |= Pattern.UNICODE_CASE; // case-insensitive Unicode flags |= Pattern.CANON_EQ; // canonicalize Unicode } else { System.err.println("Unknown option: " + option); usage();// w w w . j a va 2s . c o m } } // Get the Charset for converting bytes to chars Charset charset = Charset.forName(encodingName); // Next argument must be a regexp. Compile it to a Pattern object Pattern pattern = Pattern.compile(args[nextarg++], flags); // Require that at least one file is specified if (nextarg == args.length) usage(); // Loop through each of the specified filenames while (nextarg < args.length) { String filename = args[nextarg++]; CharBuffer chars; // This will hold complete text of the file try { // Handle per-file errors locally // Open a FileChannel to the named file FileInputStream stream = new FileInputStream(filename); FileChannel f = stream.getChannel(); // Memory-map the file into one big ByteBuffer. This is // easy but may be somewhat inefficient for short files. ByteBuffer bytes = f.map(FileChannel.MapMode.READ_ONLY, 0, f.size()); // We can close the file once it is is mapped into memory. // Closing the stream closes the channel, too. stream.close(); // Decode the entire ByteBuffer into one big CharBuffer chars = charset.decode(bytes); } catch (IOException e) { // File not found or other problem System.err.println(e); // Print error message continue; // and move on to the next file } // This is the basic regexp loop for finding all matches in a // CharSequence. Note that CharBuffer implements CharSequence. // A Matcher holds state for a given Pattern and text. Matcher matcher = pattern.matcher(chars); while (matcher.find()) { // While there are more matches // Print out details of the match System.out.println(filename + ":" + // file name matcher.start() + ": " + // character pos matcher.group()); // matching text } } } // These are the things that can go wrong in the code above catch (UnsupportedCharsetException e) { // Bad encoding name System.err.println("Unknown encoding: " + encodingName); } catch (PatternSyntaxException e) { // Bad pattern System.err.println("Syntax error in search pattern:\n" + e.getMessage()); } catch (ArrayIndexOutOfBoundsException e) { // Wrong number of arguments usage(); } }
From source file:MainClass.java
public static void main(String[] args) { long[] primes = new long[] { 1, 2, 3, 5, 7 }; File aFile = new File("C:/test/primes.bin"); FileOutputStream outputFile = null; try {/*from ww w . j ava 2 s . c om*/ outputFile = new FileOutputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel file = outputFile.getChannel(); final int BUFFERSIZE = 100; ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE); LongBuffer longBuf = buf.asLongBuffer(); int primesWritten = 0; while (primesWritten < primes.length) { longBuf.put(primes, primesWritten, min(longBuf.capacity(), primes.length - primesWritten)); buf.limit(8 * longBuf.position()); try { file.write(buf); primesWritten += longBuf.position(); } catch (IOException e) { e.printStackTrace(System.err); } longBuf.clear(); buf.clear(); } try { System.out.println("File written is " + file.size() + "bytes."); outputFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } }