标签:
package nio; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class MappedByteBufferTest { private static String sourceFileName = "UCS_PM_1.1.5_EN.sql"; private static String dstFileNameByMappedBuffer = "new.sql"; private static String dstFileNameByFileCopy = "new1.sql"; private static String dstFileNameByAllocateDirect = "new2.sql"; private static FileChannel readChannel, writeChannel; private static long start; private static long length; public static void main(String[] args) throws Exception { mapBufferWriteFile(); //使用内存映射流来完成文件复制 testFileCopy(); //使用普通的IO流来完成文件复制 allocateDirectWriteFile(); //使用DirectMemory来完成文件复制 } public static void mapBufferWriteFile() { try { start = System.currentTimeMillis(); RandomAccessFile fos = new RandomAccessFile(dstFileNameByMappedBuffer, "rw"); writeChannel = fos.getChannel(); //writeChannel.position(writeChannel.size()); append to file end position RandomAccessFile fis = new RandomAccessFile(sourceFileName, "r"); readChannel = fis.getChannel(); length = readChannel.size(); MappedByteBuffer mbb = readChannel.map(FileChannel.MapMode.READ_ONLY, 0, length); writeChannel.write(mbb); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { readChannel.close(); writeChannel.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("mapBufferWriteFile:" + (System.currentTimeMillis() - start)); } } private static void testFileCopy() { start = System.currentTimeMillis(); BufferedInputStream bis =null; BufferedOutputStream bos = null; try { bis = new BufferedInputStream(new FileInputStream(sourceFileName)); bos = new BufferedOutputStream(new FileOutputStream(dstFileNameByFileCopy)); byte[] bytes = new byte[1024]; int index = 0; int b = bis.read(); while(b != -1){ bytes[index] = (byte) b; index ++; if(index == 1023){ bos.write(bytes, 0, index); index = 0; b = bis.read(); }else{ b = bis.read(); } } if(index != 1023){ bos.write(bytes, 0, index); } } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally { try { bis.close(); bos.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("testFileCopy:" + (System.currentTimeMillis() - start)); } private static void allocateDirectWriteFile() { try { start = System.currentTimeMillis(); RandomAccessFile fos = new RandomAccessFile(dstFileNameByAllocateDirect, "rw"); writeChannel = fos.getChannel(); RandomAccessFile fis = new RandomAccessFile(sourceFileName, "r"); readChannel = fis.getChannel(); length = readChannel.size(); ByteBuffer mbb = ByteBuffer.allocate((int)readChannel.size()); readChannel.read(mbb); mbb.flip(); writeChannel.write(mbb); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { readChannel.close(); writeChannel.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("allocateDirectWriteFile:" + (System.currentTimeMillis() - start)); } } }
标签:
原文地址:http://www.cnblogs.com/lnlvinso/p/4693468.html