文件拷贝
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
public class NIOFileReadAndWrite {
private String file;
public String getFile() {
return file;
}
public void setFile(String file) {
this.file = file;
}
public NIOFileReadAndWrite(String file) throws IOException {
super();
this.file = file;
}
/**
* NIO读取文件
* @param allocate
* @throws IOException
*/
public void read(int allocate) throws IOException {
RandomAccessFile access = new RandomAccessFile(this.file, "r");
//FileInputStream inputStream = new FileInputStream(this.file);
FileChannel channel = access.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(allocate);
CharBuffer charBuffer = CharBuffer.allocate(allocate);
Charset charset = Charset.forName("GBK");
CharsetDecoder decoder = charset.newDecoder();
int length = channel.read(byteBuffer);
while (length != -1) {
byteBuffer.flip();
decoder.decode(byteBuffer, charBuffer, true);
charBuffer.flip();
System.out.println(charBuffer.toString());
// 清空缓存
byteBuffer.clear();
charBuffer.clear();
// 再次读取文本内容
length = channel.read(byteBuffer);
}
channel.close();
if (access != null) {
access.close();
}
}
/**
* NIO写文件
* @param context
* @param allocate
* @param chartName
* @throws IOException
*/
public void write(String context, int allocate, String chartName) throws IOException{
// FileOutputStream outputStream = new FileOutputStream(this.file); //文件内容覆盖模式 --不推荐
FileOutputStream outputStream = new FileOutputStream(this.file, true); //文件内容追加模式--推荐
FileChannel channel = outputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(allocate);
byteBuffer.put(context.getBytes(chartName));
byteBuffer.flip();//读取模式转换为写入模式
channel.write(byteBuffer);
channel.close();
if(outputStream != null){
outputStream.close();
}
}
/**
* nio事实现文件拷贝
* @param source
* @param target
* @param allocate
* @throws IOException
*/
public static void nioCpoy(String source, String target, int allocate) throws IOException{
ByteBuffer byteBuffer = ByteBuffer.allocate(allocate);
FileInputStream inputStream = new FileInputStream(source);
FileChannel inChannel = inputStream.getChannel();
FileOutputStream outputStream = new FileOutputStream(target);
FileChannel outChannel = outputStream.getChannel();
int length = inChannel.read(byteBuffer);
while(length != -1){
byteBuffer.flip();//读取模式转换写入模式
outChannel.write(byteBuffer);
byteBuffer.clear(); //清空缓存,等待下次写入
// 再次读取文本内容
length = inChannel.read(byteBuffer);
}
outputStream.close();
outChannel.close();
inputStream.close();
inChannel.close();
}
public static void fileChannelCopy(String sfPath, String tfPath) {
File sf = new File(sfPath);
File tf = new File(tfPath);
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try{
fi = new FileInputStream(sf);
fo = new FileOutputStream(tf);
in = fi.getChannel();//得到对应的文件通道
out = fo.getChannel();//得到对应的文件通道
in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
}catch (Exception e){
e.printStackTrace();
}finally {
try{
fi.close();
in.close();
fo.close();
out.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
//IO方法实现文件k拷贝
private static void traditionalCopy(String sourcePath, String destPath) throws Exception {
File source = new File(sourcePath);
File dest = new File(destPath);
if (!dest.exists()) {
dest.createNewFile();
}
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
}
public static void main(String[] args) throws Exception{
long start = System.currentTimeMillis();
nioCpoy("D:\\迅雷下载\\jdk-21_windows-x64_bin.zip", "D:\\qwe\\jdk-21_windows-x64_bin.zip",10240);
long end = System.currentTimeMillis();
System.out.println("用时为:" + (end-start));
long start2 = System.currentTimeMillis();
fileChannelCopy("D:\\迅雷下载\\jdk-21_windows-x64_bin.zip", "D:\\qwe\\jdk-21_windows-x64_bin.zip");
long end2 = System.currentTimeMillis();
System.out.println("用时为:" + (end2-start2));
long start3 = System.currentTimeMillis();
traditionalCopy("D:\\迅雷下载\\jdk-21_windows-x64_bin.zip", "D:\\qwe\\jdk-21_windows-x64_bin.zip");
long end3 = System.currentTimeMillis();
System.out.println("用时为:" + (end3-start3));
}
}