码迷,mamicode.com
首页 > 编程语言 > 详细

使用java NIO进行读文件

时间:2015-04-28 12:11:35      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

 public static String readUseNIO(File file) {
  FileInputStream fin;
  String string = null;
  try {
   fin = new FileInputStream(file);
   FileChannel channel = null;
   channel = fin.getChannel();
   // 文件内容的大小
   int size = (int) channel.size();
   // 获取通道
   FileChannel fc = fin.getChannel();
   // 创建缓冲区
   ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 1);
   // 读取数据到缓冲区
   fc.read(buffer);
   // Buffer bf = buffer.flip();
   // System.out.println("limt:" + bf.limit());
   byte[] bt = buffer.array();
   string = new String(bt, 0, size,"UTF-8");
   // System.out.println(new String(bt, 0, size));
   // FileUtil.appendString("F:/html/22.html", new String(bt, 0,
   // size));
   buffer.clear();
   buffer = null;
   fin.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return string;
 }

使用java NIO进行读文件

标签:

原文地址:http://my.oschina.net/u/1787735/blog/407109

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!