欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

NIO学习

程序员文章站 2022-07-04 08:45:24
...

第一个自己写的nio程序

文件复制:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Date;

public class NioClass {
    
    
    public NioClass() {
        // TODO Auto-generated constructor stub
    }
    
    public static void copyFile(String orignal, String destFile) throws IOException {
        
        
        FileInputStream fileInput = new FileInputStream(orignal);
        
        FileOutputStream fileOut = new FileOutputStream(destFile);
        
        FileChannel inputChannel = fileInput.getChannel();
        
        FileChannel outputChannel = fileOut.getChannel();
        
        ByteBuffer bf = ByteBuffer.allocate(1024);
        
        while (true) {
            
            bf.clear();
            
            int len = inputChannel.read(bf);
            
            if (len == -1) {
                break;
            }
            bf.flip();
            
            outputChannel.write(bf);
            System.out.println("执行复制");
        }
        fileInput.close();
        fileOut.close();
    }
    
    public static void main(String[] args) throws IOException {
        
        
        NioClass nc = new NioClass();
        System.out.println(new Date().getTime());
        nc.copyFile("C:\\Users\\sss\\Desktop\\ddd\\orignial.txt", "C:\\Users\\sss\\Desktop\\ddd\\dest.txt");
        System.out.println(new Date().getTime());
    }
}

上一篇: NIO学习

下一篇: NIO学习