当前位置:编程学习 > JAVA >>

java 快速读取文件的方法。

我用了Imageio.read(new File(路径)); 但是读取图片感觉还是不够快,1M的图片需要400毫米才读取完。请大神指点。 图片 --------------------编程问答-------------------- /**
     * 内存映射
     * 
     * @throws IOException
     */
    public static void mapChannel() throws IOException {
        long t1 = System.currentTimeMillis();
        FileInputStream in = new FileInputStream("d:/1.txt");
        long size = in.available();
        RandomAccessFile out = new RandomAccessFile("d:/2.txt", "rw");
        FileChannel inc = in.getChannel();
        MappedByteBuffer bf = inc.map(FileChannel.MapMode.READ_ONLY, 0, size);
        FileChannel outc = out.getChannel();
        MappedByteBuffer outbf = outc.map(FileChannel.MapMode.READ_WRITE, 0, size);
        outbf.put(bf);
        inc.close();
        outc.close();
        in.close();
        out.close();
        long t2 = System.currentTimeMillis();
        System.out.println(t2 - t1);
    }
(完全的内存映射)读文件是最快的,比任何其它读取方式快40倍,但你要BufferedImage结果那只能读到内存了 --------------------编程问答-------------------- 谢谢,那我试试啊! --------------------编程问答-------------------- 1L+1,NIO确实 --------------------编程问答-------------------- Imageio.read(new File(路径)); 
不只是读取文件,还要解析图像。 --------------------编程问答--------------------
引用 1 楼 qq331077064 的回复:
/**
     * 内存映射
     * 
     * @throws IOException
     */
    public static void mapChannel() throws IOException {
        long t1 = System.currentTimeMillis();
        FileInputStream in = new FileInputStream("d:/1.txt");
        long size = in.available();
        RandomAccessFile out = new RandomAccessFile("d:/2.txt", "rw");
        FileChannel inc = in.getChannel();
        MappedByteBuffer bf = inc.map(FileChannel.MapMode.READ_ONLY, 0, size);
        FileChannel outc = out.getChannel();
        MappedByteBuffer outbf = outc.map(FileChannel.MapMode.READ_WRITE, 0, size);
        outbf.put(bf);
        inc.close();
        outc.close();
        in.close();
        out.close();
        long t2 = System.currentTimeMillis();
        System.out.println(t2 - t1);
    }
(完全的内存映射)读文件是最快的,比任何其它读取方式快40倍,但你要BufferedImage结果那只能读到内存了

你试试比较一下使用BufferedInputStream(InputStream in, int size),把缓冲设置的大一点。与nio比较一下,给大家个回复。 --------------------编程问答-------------------- 楼主看看这个帖子:http://www.iteye.com/topic/866622 --------------------编程问答--------------------
引用 1 楼 qq331077064 的回复:
/**
     * 内存映射
     * 
     * @throws IOException
     */
    public static void mapChannel() throws IOException {
        long t1 = System.currentTimeMillis();
        FileInputStream in = new FileInputStream("d:/1.txt");
        long size = in.available();
        RandomAccessFile out = new RandomAccessFile("d:/2.txt", "rw");
        FileChannel inc = in.getChannel();
        MappedByteBuffer bf = inc.map(FileChannel.MapMode.READ_ONLY, 0, size);
        FileChannel outc = out.getChannel();
        MappedByteBuffer outbf = outc.map(FileChannel.MapMode.READ_WRITE, 0, size);
        outbf.put(bf);
        inc.close();
        outc.close();
        in.close();
        out.close();
        long t2 = System.currentTimeMillis();
        System.out.println(t2 - t1);
    }
(完全的内存映射)读文件是最快的,比任何其它读取方式快40倍,但你要BufferedImage结果那只能读到内存了

你试试比较一下使用BufferedInputStream(InputStream in, int size),把缓冲设置为1024 * 32(我的机器,你的机器可能更大一点)。与nio比较一下,给大家个回复。
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,