package test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.util.Date; public class NIOExample { public static void doByteBufferStuffWithWrap() { byte[] message = {'H', 'o', 'w', 'd', 'y', '!'}; ByteBuffer buffer = ByteBuffer.wrap(message); //buffer.flip(); don't do a flip int limit = buffer.limit(); //how many chars in buffer for (int i=0; i < limit; i++) System.out.print((char)buffer.get()); } public static void doByteBufferStuffWithAllocate() { ByteBuffer buffer = ByteBuffer.allocate(10000); byte[] message = {'H', 'o', 'w', 'd', 'y'}; buffer.put(message); buffer.put((byte) '!'); buffer.flip(); //now flip over to reading from buffer int limit = buffer.limit(); //how many chars in buffer for (int i=0; i < limit; i++) System.out.print((char)buffer.get()); } public static void doByteBufferStuffWithAllocateDirect() { ByteBuffer buffer = ByteBuffer.allocateDirect(10000); byte[] message = {'H', 'o', 'w', 'd', 'y'}; buffer.put(message); buffer.put((byte) '!'); buffer.flip(); //now flip over to reading from buffer int limit = buffer.limit(); //how many chars in buffer for (int i=0; i < limit; i++) System.out.print((char)buffer.get()); } public static void writeFile() throws IOException { boolean append = false; FileOutputStream fos = new FileOutputStream("c:/junk/junk.txt", append); FileChannel fc = fos.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); //create buffer byte[] message = {'H', 'o', 'w', 'd', 'y'}; for (int i=0; i < message.length; i++) buffer.put(message[i]); buffer.flip(); fc.write(buffer); fc.close(); } public static void writeTestFile(String filepath, int filesize) throws IOException { FileOutputStream fos = new FileOutputStream(filepath); FileChannel fc = fos.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(filesize); //create buffer byte data[] = new byte[filesize]; for (int i=0; i < data.length; i++) { data[i] = 'h'; buffer.put(data[i]); } buffer.flip(); fc.write(buffer); fc.close(); } public static void readTestFileWithIO(String filepath, int numTimes) throws IOException { File file = new File(filepath); long length = file.length(); char charBuffer[] = new char[(int)length]; for (int i=0; i < numTimes; i++) { FileReader fis = new FileReader(filepath); BufferedReader bufReader = new BufferedReader(fis); bufReader.read(charBuffer); bufReader.close(); System.out.println("IO loopCnt=" + i + ", 1st byte=" + charBuffer[0]); } } public static void readTestFileWithNIO(String filepath, int numTimes) throws IOException { File file = new File(filepath); long length = file.length(); for (int i=0; i < numTimes; i++) { FileInputStream fin = new FileInputStream(filepath); FileChannel fc = fin.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect((int)length); //create buffer fc.read(buffer); //read from channel into buffer fc.close(); buffer.flip(); System.out.println("NIO loopCnt=" + i + ", 1st byte=" + (char)buffer.asCharBuffer().charAt(0)); } } public static void main(String[] args) throws IOException { String filepath = "c:/junk/junk.dat"; writeTestFile(filepath, 5000000); System.out.println("Start IO Read:" + new Date()); readTestFileWithIO(filepath, 50); System.out.println("End IO:" + new Date() + "\n\n"); System.out.println("Start NIO Read:" + new Date()); readTestFileWithNIO(filepath, 50); System.out.println("End NIO:" + new Date() + "\n\n"); System.out.println("Exiting program..."); } }