博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中NIO对文件的读取操作
阅读量:4040 次
发布时间:2019-05-24

本文共 2542 字,大约阅读时间需要 8 分钟。

 

import .io.FileInputStream;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOOperDemo {

 public void readFileByIO(String fileName){
  FileInputStream fis=null;
  try {
    fis=new FileInputStream(fileName);
    byte[] buffer=new byte[1024];
    int len=0;
    while((len=fis.read(buffer))!=-1){
     System.out.write(buffer,0,len);
    }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    fis.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
 
 public void readByNIO(String file){
  //第一步 获取通道
  FileInputStream fis = null;
  FileChannel channel=null;
  try {
   fis = new FileInputStream(file);
   channel=fis.getChannel();
   //文件内容的大小
   int size=(int) channel.size();
   
   //第二步 指定缓冲区
      ByteBuffer buffer=ByteBuffer.allocate(1024);
   //第三步 将通道中的数据读取到缓冲区中
   channel.read(buffer);
   
   Buffer bf= buffer.flip();
   System.out.println("limt:"+bf.limit());
   
   byte[] bt=buffer.array();
   System.out.println(new String(bt,0,size));
   
   buffer.clear();
   buffer=null;
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    channel.close();
    fis.close();
    
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  
   
  }
  
 }

 /**

  * 利用NIO将内容输出到文件中
  * @param file
  */
 public void writeFileByNIO(String file){
         FileOutputStream fos=null;
         FileChannel fc=null;
         ByteBuffer buffer=null;
         try {
   fos=new FileOutputStream(file);
    //第一步 获取一个通道
   fc=fos.getChannel();
   //buffer=ByteBuffer.allocate(1024);
    //第二步 定义缓冲区
   buffer=ByteBuffer.wrap("Hello World 2".getBytes());
    //将内容写到缓冲区
   fos.flush();
   fc.write(buffer);
   
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    fc.close();
    fos.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }
 }

 public static void main(String[] args) {

  NIOOperDemo nood=new NIOOperDemo();
  String fileName="c:\\hello.txt";
  //nood.readFileByIO(fileName);
  //nood.readByNIO(fileName);
  nood.writeFileByNIO(fileName);
 }
 
 
 
 

}

你可能感兴趣的文章
IDEA Properties中文unicode转码问题
查看>>
Idea下安装Lombok插件
查看>>
zookeeper
查看>>
Idea导入的工程看不到src等代码
查看>>
技术栈
查看>>
Jenkins中shell-script执行报错sh: line 2: npm: command not found
查看>>
8.X版本的node打包时,gulp命令报错 require.extensions.hasownproperty
查看>>
Jenkins 启动命令
查看>>
Maven项目版本继承 – 我必须指定父版本?
查看>>
Maven跳过单元测试的两种方式
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
[leetcode BY python]1两数之和
查看>>
微信小程序开发全线记录
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>