Java文件復(fù)制多種方法實(shí)例代碼
1、InputStream與OutputStream
創(chuàng)建兩個(gè)文件 - 源和目標(biāo)。然后我們從源創(chuàng)建InputStream并使用OutputStream將其寫(xiě)入目標(biāo)文件進(jìn)行 java 復(fù)制文件操作。
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 中復(fù)制文件。如果您已經(jīng)在項(xiàng)目中使用 Apache Commons IO,那么使用它來(lái)簡(jiǎn)化代碼是有意義的。它在內(nèi)部使用 Java NIO FileChannel,因此如果您尚未將其用于其他功能,則可以避免使用此包裝器方法。下面是使用apache commons io進(jìn)行java復(fù)制文件操作的方法
private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
FileUtils.copyFile(source, dest);
}3、Files類的copy()方法在 java 中復(fù)制文件
private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}4、使用BufferedInputStream/BufferedOutputStream高效字節(jié)流進(jìn)行復(fù)制文件
private static void bufferedStreamCopyFile(File srcFile, File desFile) throwsIOException {
//使用緩沖字節(jié)流進(jìn)行文件復(fù)制
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字符流進(jìn)行文件復(fù)制
注意這種方式只能復(fù)制只包含字符的文件,也就意味著你用記事本打開(kāi)該文件你能夠讀懂
private static void readerWriterCopyFile(File srcFile, File desFile) throwsIOException {
//使用字符流進(jìn)行文件復(fù)制,注意:字符流只能復(fù)制只含有漢字的文件
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高效字符流進(jìn)行文件復(fù)制
意這種方式只能復(fù)制只包含字符的文件,也就意味著你用記事本打開(kāi)該文件你能夠讀懂
private static void bufferedReaderWriterCopyFile(File srcFile, File desFile) throwsIOException {
//使用帶緩沖區(qū)的高效字符流進(jìn)行文件復(fù)制
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高效字符流進(jìn)行文件復(fù)制
注意這種方式只能復(fù)制只包含字符的文件,也就意味著你用記事本打開(kāi)該文件你能夠讀懂
private static void bufferedReaderWriterCopyFile(File srcFile, File desFile) throwsIOException {
//使用帶緩沖區(qū)的高效字符流進(jìn)行文件復(fù)制
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復(fù)制
Java NIO包括transferFrom方法,根據(jù)文檔應(yīng)該比文件流復(fù)制的速度更快
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();
}
} 總結(jié)
到此這篇關(guān)于Java文件復(fù)制多種方法的文章就介紹到這了,更多相關(guān)Java文件復(fù)制方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java字符串的大寫(xiě)字母右移實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇java字符串的大寫(xiě)字母右移實(shí)現(xiàn)方法。小編覺(jué)得聽(tīng)不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
IDEA創(chuàng)建Java?Web項(xiàng)目的超詳細(xì)圖文教學(xué)
IDEA是程序員們常用的java集成開(kāi)發(fā)環(huán)境,也是被公認(rèn)為最好用的java開(kāi)發(fā)工具,下面這篇文章主要給大家介紹了關(guān)于IDEA創(chuàng)建Java?Web項(xiàng)目的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
MyBatis-Plus找不到Mapper.xml文件的幾種解決方法
mybatis-plus今天遇到一個(gè)問(wèn)題,就是mybatis 沒(méi)有讀取到mapper.xml 文件,所以下面這篇文章主要給大家介紹了關(guān)于MyBatis-Plus找不到Mapper.xml文件的幾種解決方法,需要的朋友可以參考下2022-06-06
基于Spring整合mybatis注解掃描是否成功的問(wèn)題
這篇文章主要介紹了Spring整合mybatis注解掃描是否成功的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
關(guān)于Java中常見(jiàn)的負(fù)載均衡算法
這篇文章主要介紹了關(guān)于Java中常見(jiàn)的負(fù)載均衡算法,負(fù)載平衡是一種電子計(jì)算機(jī)技術(shù),用來(lái)在多個(gè)計(jì)算機(jī)、網(wǎng)絡(luò)連接、CPU、磁盤(pán)驅(qū)動(dòng)器或其他資源中分配負(fù)載,以達(dá)到優(yōu)化資源使用、最大化吞吐率、最小化響應(yīng)時(shí)間、同時(shí)避免過(guò)載的目的,需要的朋友可以參考下2023-08-08
Java9版本新特性同一個(gè)Jar支持多JDK版本運(yùn)行
這篇文章主要為大家介紹了Java9新版本的特性之同一個(gè)Jar支持多JDK版本運(yùn)行的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03

