java 快速读取文件的方法。

the_chose_one 2013-09-02 02:51:38
我用了Imageio.read(new File(路径)); 但是读取图片感觉还是不够快,1M的图片需要400毫米才读取完。请大神指点。
...全文
400 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jdgdf566 2013-12-02
  • 打赏
  • 举报
回复
引用 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比较一下,给大家个回复。
jdgdf566 2013-12-02
  • 打赏
  • 举报
回复
楼主看看这个帖子:http://www.iteye.com/topic/866622
jdgdf566 2013-12-02
  • 打赏
  • 举报
回复
引用 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比较一下,给大家个回复。
Inhibitory 2013-12-02
  • 打赏
  • 举报
回复
Imageio.read(new File(路径)); 不只是读取文件,还要解析图像。
sca4441479 2013-12-02
  • 打赏
  • 举报
回复
1L+1,NIO确实
the_chose_one 2013-12-01
  • 打赏
  • 举报
回复
谢谢,那我试试啊!
郑涛 2013-09-03
  • 打赏
  • 举报
回复
/** * 内存映射 * * @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结果那只能读到内存了

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧