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:Main.java
public static void main(String[] argv) throws Exception { File file = new File("filename"); // Create a read-only memory-mapped file FileChannel roChannel = new RandomAccessFile(file, "r").getChannel(); ByteBuffer readonlybuffer = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.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(); MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size()); buf.put(0, (byte) 0xFF); System.out.println(buf.isLoaded()); buf.force();/*www . j a va 2 s.c om*/ channel.close(); }
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); System.out.println(buf.isLoaded()); buf.force();/*from ww w .j ava 2 s.c om*/ MappedByteBuffer mbb = buf.load(); 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[] args) throws Exception { FileInputStream fis = new FileInputStream("test.txt"); FileChannel fc = fis.getChannel(); long startRegion = 0; long endRegion = fc.size(); MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, startRegion, endRegion); while (mbb.hasRemaining()) { System.out.print((char) mbb.get()); }//from w ww . j a v a2 s .c om fis.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:MappedChannelWrite.java
public static void main(String args[]) { RandomAccessFile fOut;/*from ww w .ja va 2s.c om*/ FileChannel fChan; ByteBuffer mBuf; try { fOut = new RandomAccessFile("test.txt", "rw"); fChan = fOut.getChannel(); mBuf = fChan.map(FileChannel.MapMode.READ_WRITE, 0, 26); for (int i = 0; i < 26; i++) mBuf.put((byte) ('A' + i)); fChan.close(); fOut.close(); } catch (IOException exc) { System.out.println(exc); 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:WordCount.java
public static void main(String args[]) throws Exception { String filename = "WordCount.java"; // Map File from filename to byte buffer FileInputStream input = new FileInputStream(filename); FileChannel channel = input.getChannel(); int fileLength = (int) channel.size(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileLength); // Convert to character buffer Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharBuffer charBuffer = decoder.decode(buffer); // Create line pattern Pattern linePattern = Pattern.compile(".*$", Pattern.MULTILINE); // Create word pattern Pattern wordBreakPattern = Pattern.compile("[\\p{Punct}\\s}]"); // Match line pattern to buffer Matcher lineMatcher = linePattern.matcher(charBuffer); Map map = new TreeMap(); Integer ONE = new Integer(1); // For each line while (lineMatcher.find()) { // Get line CharSequence line = lineMatcher.group(); // Get array of words on line String words[] = wordBreakPattern.split(line); // For each word for (int i = 0, n = words.length; i < n; i++) { if (words[i].length() > 0) { Integer frequency = (Integer) map.get(words[i]); if (frequency == null) { frequency = ONE;//w ww .j av a 2 s .c o m } else { int value = frequency.intValue(); frequency = new Integer(value + 1); } map.put(words[i], frequency); } } } System.out.println(map); }
From source file:MainClass.java
public static void main(String args[]) { RandomAccessFile randomAccessFile; FileChannel fileChannel; ByteBuffer byteBuffer;//from w ww. j a va 2 s .com 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); } }