List of usage examples for java.nio.channels FileChannel map
public abstract MappedByteBuffer map(MapMode mode, long position, long size) throws IOException;
From source file:MappedChannelRead.java
public static void main(String args[]) { FileInputStream fIn;// w w w . j a v a 2 s. c o m FileChannel fChan; long fSize; MappedByteBuffer mBuf; try { fIn = new FileInputStream("test.txt"); fChan = fIn.getChannel(); fSize = fChan.size(); mBuf = fChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize); for (int i = 0; i < fSize; i++) System.out.print((char) mBuf.get()); fChan.close(); fIn.close(); } catch (IOException exc) { System.out.println(exc); System.exit(1); } }
From source file:MainClass.java
public static void main(String args[]) { FileInputStream fileInputStream; FileChannel fileChannel; long fileSize; MappedByteBuffer mBuf;/*from w ww. ja v a2 s. c om*/ 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:NIOCopy.java
public static void main(String args[]) throws Exception { FileInputStream fIn;//from w w w. j a va2 s . c o m 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: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 va2 s . 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:MainClass.java
public static void main(String args[]) { FileInputStream fIn;/*from ww w. ja v a 2 s .c o 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: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();/* ww w . java 2 s. 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:Main.java
public static CharSequence fromFile(String filename) throws IOException { FileInputStream fis = new FileInputStream(filename); FileChannel fc = fis.getChannel(); // Create a read-only CharBuffer on the file ByteBuffer bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size()); CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf); return cbuf;/*from ww w . j a va 2s .c o m*/ }
From source file:Main.java
public static String readFile(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); String str = null;// w w w. j a v a 2s . c o m try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ str = Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } return str; }
From source file:Main.java
public static ByteBuffer mapFile(File f, long offset, ByteOrder byteOrder) throws IOException { FileInputStream dataFile = new FileInputStream(f); try {//from ww w .ja va2s .c o m FileChannel fc = dataFile.getChannel(); MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, offset, f.length() - offset); buffer.order(byteOrder); return buffer; } finally { dataFile.close(); // this *also* closes the associated channel, fc } }
From source file:Main.java
public static String getFileMD5String(File file) throws IOException { FileInputStream in = new FileInputStream(file); FileChannel ch = in.getChannel(); MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); messagedigest.update(byteBuffer);// w w w . j a v a 2s . co m return bufferToHex(messagedigest.digest()); }