Java文件復制多種方法實例代碼
1、InputStream與OutputStream
創(chuàng)建兩個文件 - 源和目標。然后我們從源創(chuàng)建InputStream并使用OutputStream將其寫入目標文件進行 java 復制文件操作。
private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}2、Apache Commons IO FileUtils
copyFile(File srcFile, File destFile)可用于在 java 中復制文件。如果您已經在項目中使用 Apache Commons IO,那么使用它來簡化代碼是有意義的。它在內部使用 Java NIO FileChannel,因此如果您尚未將其用于其他功能,則可以避免使用此包裝器方法。下面是使用apache commons io進行java復制文件操作的方法
private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
FileUtils.copyFile(source, dest);
}3、Files類的copy()方法在 java 中復制文件
private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}4、使用BufferedInputStream/BufferedOutputStream高效字節(jié)流進行復制文件
private static void bufferedStreamCopyFile(File srcFile, File desFile) throwsIOException {
//使用緩沖字節(jié)流進行文件復制
BufferedInputStream bis = new BufferedInputStream(newFileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(newFileOutputStream(desFile));
byte[] b = new byte[1024];
Integer len = 0;
//一次讀取1024字節(jié)的數(shù)據(jù)
while((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
bis.close();
bos.close();
}5、使用FileReader/FileWriter字符流進行文件復制
注意這種方式只能復制只包含字符的文件,也就意味著你用記事本打開該文件你能夠讀懂
private static void readerWriterCopyFile(File srcFile, File desFile) throwsIOException {
//使用字符流進行文件復制,注意:字符流只能復制只含有漢字的文件
FileReader fr = newFileReader(srcFile);
FileWriter fw = newFileWriter(desFile);
Integer by = 0;
while((by = fr.read()) != -1) {
fw.write(by);
}
fr.close();
fw.close();
}6、使用BufferedReader/BufferedWriter高效字符流進行文件復制
意這種方式只能復制只包含字符的文件,也就意味著你用記事本打開該文件你能夠讀懂
private static void bufferedReaderWriterCopyFile(File srcFile, File desFile) throwsIOException {
//使用帶緩沖區(qū)的高效字符流進行文件復制
BufferedReader br = new BufferedReader(newFileReader(srcFile));
BufferedWriter bw = new BufferedWriter(newFileWriter(desFile));
char[] c = new char[1024];
Integer len = 0;
while((len = br.read(c)) != -1) {
bw.write(c, 0, len);
}
//方式二
/*String s = null;
while((s = br.readLine()) != null) {
bw.write(s);
bw.newLine();
}*/
br.close();
bw.close();
}7、使用BufferedReader/BufferedWriter高效字符流進行文件復制
注意這種方式只能復制只包含字符的文件,也就意味著你用記事本打開該文件你能夠讀懂
private static void bufferedReaderWriterCopyFile(File srcFile, File desFile) throwsIOException {
//使用帶緩沖區(qū)的高效字符流進行文件復制
BufferedReader br = new BufferedReader(newFileReader(srcFile));
BufferedWriter bw = new BufferedWriter(newFileWriter(desFile));
char[] c = new char[1024];
Integer len = 0;
while((len = br.read(c)) != -1) {
bw.write(c, 0, len);
}
//方式二
/*String s = null;
while((s = br.readLine()) != null) {
bw.write(s);
bw.newLine();
}*/
br.close();
bw.close();
}8、使用FileChannel復制
Java NIO包括transferFrom方法,根據(jù)文檔應該比文件流復制的速度更快
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();
}
} 總結
到此這篇關于Java文件復制多種方法的文章就介紹到這了,更多相關Java文件復制方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Maven管理多模塊應用的統(tǒng)一版本號實現(xiàn)
本文主要介紹了Maven管理多模塊應用的統(tǒng)一版本號實現(xiàn),使用versions-maven-plugin插件和占位符結合flatten-maven-plugin插件來實現(xiàn),感興趣的可以了解一下2024-12-12
IntelliJ IDEA進行遠程調試(Remote Debugging)的操作教程
遠程調試(Remote Debugging)是指在本地開發(fā)環(huán)境(如 IntelliJ IDEA)中,連接并調試運行在遠程機器(如測試服務器、預發(fā)環(huán)境、生產服務器、Docker 容器、Kubernetes Pod 等)上的 Java 應用程序,本文給大家介紹了IntelliJ IDEA進行遠程調試的操作教程2025-11-11
Springboot?RestTemplate設置超時時間的簡單方法
學習springboot ,RestTemplate的使用場景非常非常多,比如springcloud中的服務消費,下面這篇文章主要給大家介紹了關于Springboot?RestTemplate設置超時時間的簡單方法,需要的朋友可以參考下2022-01-01

