日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java直接调用复制文件,java中文件复制的4种方式,java文件的复制

發布時間:2025/3/20 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java直接调用复制文件,java中文件复制的4种方式,java文件的复制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java中文件復制的4種方式,java文件的復制

今天一個同事問我文件復制的問題,他一個100M的文件復制的指定目錄下竟然成了1G多,嚇我一跳,后來看了他的代碼發現是自己通過字節流復制的,定義的字節數組很大,導致復制后目標文件非常大,其實就是空行等一些無效空間。我也是很少用這種方式拷貝問價,大多數用Apache提供的commons-io中的FileUtils,后來在網上查了下,發現還有其他的方式,效率更高,所以在此整理一下,也是自己的一個學習。

1. 使用FileStreams復制

比較經典的一個代碼,使用FileInputStream讀取文件A的字節,使用FileOutputStream寫入到文件B。

2、使用FileChannel

Java NIO包括transferFrom方法,根據文檔應該比文件流復制的速度更快。

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.nio.channels.FileChannel;

import java.nio.file.Files;

import org.apache.commons.io.FileUtils;

public class CopyFilesExample {

public static void main(String[] args)throws InterruptedException,

IOException {

File source =new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile1.txt");

File dest =new File("C:\\Users\\nikos7\\Desktop\\files\\destfile1.txt");

// copy file using FileStreams

long start = System.nanoTime();

long end;

copyFileUsingFileStreams(source, dest);

System.out.println("Time taken by FileStreams Copy = "

+ (System.nanoTime() - start));

// copy files using java.nio.FileChannel

source =new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile2.txt");

dest =new File("C:\\Users\\nikos7\\Desktop\\files\\destfile2.txt");

start = System.nanoTime();

copyFileUsingFileChannels(source, dest);

end = System.nanoTime();

System.out.println("Time taken by FileChannels Copy = " + (end - start));

// copy file using Java 7 Files class

source =new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile3.txt");

dest =new File("C:\\Users\\nikos7\\Desktop\\files\\destfile3.txt");

start = System.nanoTime();

copyFileUsingJava7Files(source, dest);

end = System.nanoTime();

System.out.println("Time taken by Java7 Files Copy = " + (end - start));

// copy files using apache commons io

source =new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile4.txt");

dest =new File("C:\\Users\\nikos7\\Desktop\\files\\destfile4.txt");

start = System.nanoTime();

copyFileUsingApacheCommonsIO(source, dest);

end = System.nanoTime();

System.out.println("Time taken by Apache Commons IO Copy = "

+ (end - start));

}

private static void copyFileUsingFileStreams(File source, File dest)

throws IOException {

InputStream input =null;

OutputStream output =null;

try {

input =new FileInputStream(source);

output =new FileOutputStream(dest);

byte[] buf =new byte[1024];

int bytesRead;

while ((bytesRead = input.read(buf)) >0) {

output.write(buf,0, bytesRead);

}

}finally {

input.close();

output.close();

}

}

private static void copyFileUsingFileChannels(File source, File dest)

throws IOException {

FileChannel inputChannel =null;

FileChannel outputChannel =null;

try {

inputChannel =new FileInputStream(source).getChannel();

outputChannel =new FileOutputStream(dest).getChannel();

outputChannel.transferFrom(inputChannel,0, inputChannel.size());

}finally {

inputChannel.close();

outputChannel.close();

}

}

private static void copyFileUsingJava7Files(File source, File dest)

throws IOException {

Files.copy(source.toPath(), dest.toPath());

}

private static void copyFileUsingApacheCommonsIO(File source, File dest)

throws IOException {

FileUtils.copyFile(source, dest);

}

}

輸出:

Time taken by FileStreams Copy =127572360

Time taken by FileChannels Copy =10449963

Time taken by Java7 Files Copy =10808333

Time taken by Apache Commons IO Copy =17971677

相關文章暫無相關文章

總結

以上是生活随笔為你收集整理的java直接调用复制文件,java中文件复制的4种方式,java文件的复制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。