java nio FileChannel操作文件

说明

本文介绍 2 种方式创建 FileChannel 对象,并且通过该对象实现读写文件

方式一:通过 IO 流

通过下面 Stream 获取 FileChannel:

  • FileInputStream
  • FileOutputStream
  • RandomAccessFile

例子:读取文件

package com.std;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Read {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("d:\\1.txt");
        int len = fis.available();
        ByteBuffer bb = ByteBuffer.allocate(len);
        FileChannel fc = fis.getChannel();
        fc.read(bb);
        fc.close();

//        bb.flip();
//        byte[] buf = new byte[len];
//        bb.get(buf);
//        String content = new String(buf);
        String content = new String(bb.array());
        System.out.println(content);
    }
}

例子

复制文件

FileInputStream fis=null;
FileOutputStream fos=null;
FileChannel ifc=null;
FileChannel ofc=null;
try {
    fis=new FileInputStream("C:\\Users\\mym\\Desktop\\1.txt");
    fos=new FileOutputStream("C:\\Users\\mym\\Desktop\\2.txt");
    ifc=fis.getChannel();
    ofc=fos.getChannel();

    ByteBuffer bb=ByteBuffer.allocate(1024);
    while(ifc.read(bb)!=-1) {
        bb.flip();
        ofc.write(bb);
        bb.clear();
    }
}catch(Exception e) {
    e.printStackTrace();
}finally {
    if(ifc!=null) {
        try {
            ifc.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(ofc!=null) {
        try {
            ofc.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(fis!=null) {
        try {
            fis.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(fos!=null) {
        try {
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

方式二:通过 FileChannel

通过 FileChannel.open() 创建对象

例子

FileChannel ifc=null;
FileChannel ofc=null;
String src="C:\\Users\\mym\\Desktop\\1.txt";
String dst="C:\\Users\\mym\\Desktop\\2.txt";
try {
    ifc=FileChannel.open(Paths.get(src),StandardOpenOption.READ);
    ofc=FileChannel.open(Paths.get(dst),StandardOpenOption.WRITE,StandardOpenOption.CREATE);

    ByteBuffer bb=ByteBuffer.allocate(1024);
    while(ifc.read(bb)!=-1) {
        bb.flip();
        ofc.write(bb);
        bb.clear();
    }
}catch(Exception e) {
    e.printStackTrace();
}finally {
    if(ifc!=null) {
        try {
            ifc.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(ofc!=null) {
        try {
            ofc.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

使用通道复制文件

FileChannel ifc=null;
FileChannel ofc=null;
String src="C:\\Users\\mym\\Desktop\\1.txt";
String dst="C:\\Users\\mym\\Desktop\\2.txt";
try {
    ifc=FileChannel.open(Paths.get(src),StandardOpenOption.READ);
    ofc=FileChannel.open(Paths.get(dst),StandardOpenOption.WRITE,StandardOpenOption.CREATE);
    ifc.transferTo(0, ifc.size(), ofc);
}catch(Exception e){
    e.printStackTrace();
}finally {
    if(ifc!=null) {
        try {
            ifc.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    if(ofc!=null) {
        try {
            ofc.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

原文出处:/show_1IX1bA8YWT4T.html