Driect-nonDricect 读取文件速度

 

fos.write() 7965 ms

fileChannel.transferTo 170 ms

 

MappedByteBuffer out = fc.map(FileChannel.MapMode.READ_WRITE, 0, length);  
raf = new RandomAccessFile( metaFile, "rw" );
FileChannel channel = raf.getChannel();
channel.force( true );	//強制將所有對此通道的檔案更新寫入包含該檔案的存儲設備中。如果此通道的檔案駐留在本地存儲設備上,則此方法返回時可保證,否则不保证。
MappedByteBuffer buf = channel.map( FileChannel.MapMode.READ_WRITE, 0, metadataMaxLenght );

 

 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;

public class TestFileChannel {
    public static void main( String[] args ) throws Exception {
        
    }
    
    public static void a() throws Exception{
        File file = new File("d:/bcw-200.log");
        FileInputStream fis = new FileInputStream( file );
        File outFile = new File("d:/bcw-201.log");
        FileOutputStream fos = new FileOutputStream( outFile );
        int byteToRead = 1024;
        byte[] buff = new byte[byteToRead];
//        int byteRead = 0;
//        while(true){
//            int result = fis.read( bb, byteRead, byteToRead - byteRead );
//            if(result==-1){
//                break;
//            }
//            byteRead += result;
//        }
        long start = System.currentTimeMillis();
        while(true){
            int result = fis.read( buff );
            fos.write( buff );
            if(result == -1){
                break;
            }
        }
        System.out.println(System.currentTimeMillis() - start);
        fis.close();
        fos.close();
        System.out.println(System.currentTimeMillis() - start);
    }
    
    public static void b() throws Exception{
        File file = new File("d:/bcw-200.log");
        FileInputStream fis = new FileInputStream( file );
        File outFile = new File("d:/bcw-202.log");
        FileOutputStream fos = new FileOutputStream( outFile );
        FileChannel in = fis.getChannel();
        FileChannel out = fos.getChannel();
        long start = System.currentTimeMillis();
        in.transferTo( 0, file.length(), out );
        System.out.println(System.currentTimeMillis() - start);
        in.close();
        out.close();
        System.out.println(System.currentTimeMillis() - start);
    }
    
    public static void c() throws Exception{
        File file = new File("d:/bcw-200.log");
        FileInputStream fis = new FileInputStream( file );
        File outFile = new File("d:/bcw-202.log");
        FileOutputStream fos = new FileOutputStream( outFile );
        FileChannel in = fis.getChannel();
        FileChannel out = fos.getChannel();
        RandomAccessFile raf = new RandomAccessFile( file, "rw" );
       
        long start = System.currentTimeMillis();
        in.transferTo( 0, file.length(), out );
        System.out.println(System.currentTimeMillis() - start);
        in.close();
        out.close();
        System.out.println(System.currentTimeMillis() - start);
    }
}

 

你可能感兴趣的:(读取文件)