List of usage examples for java.io FileInputStream getChannel
public FileChannel getChannel()
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()); }//w w w. j a va2s. c o m fis.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { FileInputStream fIn = new FileInputStream("test.txt"); FileChannel fChan = fIn.getChannel(); long fSize = fChan.size(); ByteBuffer mBuf = ByteBuffer.allocate((int) fSize); fChan.read(mBuf, 10);/*from w w w. ja va2 s . c o m*/ mBuf.rewind(); for (int i = 0; i < fSize; i++) { System.out.print((char) mBuf.get()); } fChan.close(); fIn.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { FileInputStream fIn = new FileInputStream("test.txt"); FileChannel fChan = fIn.getChannel(); long fSize = fChan.size(); ByteBuffer mBuf = ByteBuffer.allocate((int) fSize); fChan.read(mBuf);//from w w w . j a v a 2s. c o m mBuf.rewind(); for (int i = 0; i < fSize; i++) { System.out.print((char) mBuf.get()); } fChan.close(); fIn.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { File aFile = new File("C:/test.bin"); FileInputStream inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); final int PRIMECOUNT = 6; ByteBuffer buf = ByteBuffer.allocate(8 * PRIMECOUNT); long[] primes = new long[PRIMECOUNT]; while (inChannel.read(buf) != -1) { ((ByteBuffer) (buf.flip())).asLongBuffer().get(primes); for (long prime : primes) { System.out.printf("%10d", prime); }// www. j a v a 2s . c o m buf.clear(); } inFile.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Pattern pattern = Pattern.compile("pattern"); FileInputStream input = new FileInputStream("file.txt"); FileChannel channel = input.getChannel(); ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, (int) channel.size()); CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf); Matcher matcher = pattern.matcher(cbuf); while (matcher.find()) { String match = matcher.group(); System.out.println(match); }/*from ww w . jav a2s .com*/ }
From source file:Main.java
public static void main(String args[]) throws IOException { FileInputStream fis = new FileInputStream("FileChannelExample.java"); FileChannel fc = fis.getChannel(); ByteBuffer bb = ByteBuffer.allocate((int) fc.size()); fc.read(bb);/* www . j ava2 s. c o m*/ bb.flip(); String fileContent = new String(bb.array()); fc.close(); fc = null; System.out.println("fileContent = " + fileContent); }
From source file:Main.java
public static void main(String[] args) throws Exception { File aFile = new File("main.java"); FileInputStream inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); ByteBuffer lengthBuf = ByteBuffer.allocate(8); while (true) { if (inChannel.read(lengthBuf) == -1) { break; }/* ww w . ja v a 2 s .c o m*/ lengthBuf.flip(); int strLength = (int) lengthBuf.getDouble(); ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8); if (inChannel.read(buf) == -1) { break; } buf.flip(); byte[] strChars = new byte[2 * strLength]; buf.get(strChars); System.out.println(strLength); System.out.println(ByteBuffer.wrap(strChars).asCharBuffer()); System.out.println(buf.getLong()); lengthBuf.clear(); } inFile.close(); }
From source file:BufferConverter.java
public static void main(String[] arguments) { try {//from w ww . j a v a2 s.com String data = "friends.dat"; FileInputStream inData = new FileInputStream(data); FileChannel inChannel = inData.getChannel(); long inSize = inChannel.size(); ByteBuffer source = ByteBuffer.allocate((int) inSize); inChannel.read(source, 0); source.position(0); for (int i = 0; source.remaining() > 0; i++) System.out.print(source.get() + " "); source.position(0); Charset ascii = Charset.forName("US-ASCII"); CharsetDecoder toAscii = ascii.newDecoder(); CharBuffer destination = toAscii.decode(source); destination.position(0); System.out.println("\n\nNew character data:"); for (int i = 0; destination.remaining() > 0; i++) System.out.print(destination.get()); } catch (Exception ioe) { System.out.println(ioe.getMessage()); } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { File aFile = new File("charData.xml"); FileInputStream inFile = null; inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(48); while (inChannel.read(buf) != -1) { System.out.println("String read: " + ((ByteBuffer) (buf.flip())).asCharBuffer().get(0)); buf.clear();/*from w ww .ja v a 2 s .c om*/ } inFile.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { File aFile = new File("primes.txt"); FileInputStream inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(1024); buf.position(buf.limit());// www .jav a 2s . c om while (true) { if (buf.remaining() < 8) { if (inChannel.read(buf.compact()) == -1) { break; } buf.flip(); } int strLength = (int) buf.getDouble(); if (buf.remaining() < 2 * strLength) { if (inChannel.read(buf.compact()) == -1) { break; } buf.flip(); } byte[] strChars = new byte[2 * strLength]; buf.get(strChars); if (buf.remaining() < 8) { if (inChannel.read(buf.compact()) == -1) { break; } buf.flip(); } System.out.println(strLength); System.out.println(ByteBuffer.wrap(strChars).asCharBuffer()); System.out.println(buf.getLong()); } inFile.close(); }