- 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;
- }
来源: http://www.phpxs.com/code/1001555/