java實(shí)現(xiàn)文件拷貝的七種方式
1. 通過(guò)字節(jié)流實(shí)現(xiàn)文件的拷貝
/**
* 通過(guò)字節(jié)流實(shí)現(xiàn)文件的拷貝
* @param sourcePath 源文件路徑
* @param targetPath 目標(biāo)文件路徑
*/
public static void copyFileByStream(String sourcePath,String targetPath){
//源文件路徑
File source = new File(sourcePath);
//目標(biāo)文件路徑
File target = new File(targetPath);
//如果源文件不存在則不能拷貝
if(!source.exists()){
return;
}
//如果目標(biāo)文件目錄不存在則創(chuàng)建
if(!target.getParentFile().exists()){
target.getParentFile().mkdirs();
}
try {
//實(shí)現(xiàn)文件的拷貝
InputStream inputStream = new FileInputStream(source);
OutputStream outputStream = new FileOutputStream(target);
int temp = 0;
//每次讀取1024個(gè)字節(jié)
byte[] data = new byte[1024];
//將每次讀取的數(shù)據(jù)保存到字節(jié)數(shù)組里面,并且返回讀取的個(gè)數(shù)
while ((temp = inputStream.read(data)) != -1){
//輸出數(shù)組
outputStream.write(data,0,temp);
}
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2. 通過(guò)字符流實(shí)現(xiàn)文件拷貝
使用字符流只能拷貝文本文件
/**
* 通過(guò)字符流實(shí)現(xiàn)文件的拷貝
*
* @param sourcePath 源文件路徑
* @param targetPath 目標(biāo)文件路徑
*/
public static void copyFileByReaderAndWriter(String sourcePath, String targetPath) {
//源文件路徑
File source = new File(sourcePath);
//目標(biāo)文件路徑
File target = new File(targetPath);
//如果源文件不存在則不能拷貝
if (!source.exists()) {
return;
}
//如果目標(biāo)文件目錄不存在則創(chuàng)建
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
}
FileReader in = null;
FileWriter out = null;
try {
//字符輸入流和字符輸出流
in = new FileReader(source);
out = new FileWriter(target);
char[] c = new char[1024];
int temp = 0;
//每次讀取1024個(gè)字符
while ((temp = in.read(c)) != -1) {
//輸出到文件
out.write(c, 0, temp);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//關(guān)閉流
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. 通過(guò)字節(jié)緩沖流實(shí)現(xiàn)文件拷貝
/**
* 通過(guò)字節(jié)緩沖流實(shí)現(xiàn)文件的拷貝
*
* @param sourcePath 源文件路徑
* @param targetPath 目標(biāo)文件路徑
*/
public static void copyFileByBuffered(String sourcePath, String targetPath){
//源文件路徑
File source = new File(sourcePath);
//目標(biāo)文件路徑
File target = new File(targetPath);
//如果源文件不存在則不能拷貝
if (!source.exists()) {
return;
}
//如果目標(biāo)文件目錄不存在則創(chuàng)建
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
}
InputStream in = null;
OutputStream out = null;
try {
//字節(jié)緩沖輸入流和字節(jié)緩沖輸出流
in = new BufferedInputStream(new FileInputStream(source));
out = new BufferedOutputStream(new FileOutputStream(target));
byte[] b = new byte[1024];
int temp = 0;
//每次讀取一個(gè)1024的字節(jié)數(shù)組
while((temp = in.read(b)) != -1){
//輸出到文件
out.write(b,0,temp);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//關(guān)閉流
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4. 通過(guò)字符緩沖流拷貝文件
字符緩沖流只能讀取文本文件
/**
* 通過(guò)字符緩沖流實(shí)現(xiàn)文件的拷貝
*
* @param sourcePath 源文件路徑
* @param targetPath 目標(biāo)文件路徑
*/
public static void copyFileByBufferedChar(String sourcePath, String targetPath){
//源文件路徑
File source = new File(sourcePath);
//目標(biāo)文件路徑
File target = new File(targetPath);
//如果源文件不存在則不能拷貝
if (!source.exists()) {
return;
}
//如果目標(biāo)文件目錄不存在則創(chuàng)建
if (!target.getParentFile().exists()) {
target.getParentFile().mkdirs();
}
BufferedReader in = null;
BufferedWriter out = null;
try {
//字符緩沖輸入流和字符緩沖輸出流
in = new BufferedReader(new FileReader(source));
out = new BufferedWriter(new FileWriter(target));
//讀取文件(每次讀取一行)
String temp = null;
while((temp = in.readLine()) != null){
//輸出到文件
out.write(temp);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//關(guān)閉流
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
5. 通過(guò)JAVA NIO 非直接緩沖區(qū)拷貝文件
/**
* 通過(guò)JAVA NIO 非直接緩沖區(qū)拷貝文件
*
* @param sourcePath 源文件路徑
* @param targetPath 目標(biāo)文件路徑
*/
public static void copyFileByChannel(String sourcePath, String targetPath) {
FileChannel outChannel = null;
FileChannel inChannel = null;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(sourcePath);
fos = new FileOutputStream(targetPath);
//獲取通道
inChannel = fis.getChannel();
outChannel = fos.getChannel();
//分配指定大小的緩沖區(qū)
ByteBuffer buf = ByteBuffer.allocate(1024);
while (inChannel.read(buf) != -1) {
//轉(zhuǎn)換為讀取數(shù)據(jù)模式
buf.flip();
//寫(xiě)入到磁盤(pán)
outChannel.write(buf);
//清空緩沖區(qū)
buf.clear();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//關(guān)閉流
try {
if (outChannel != null) {
outChannel.close();
}
if (inChannel != null) {
inChannel.close();
}
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
6. 通過(guò)JAVA NIO 直接緩沖區(qū)拷貝文件
/**
* 通過(guò)JAVA NIO 直接緩沖區(qū)拷貝文件(內(nèi)存映射文件)
*
* @param sourcePath 源文件路徑
* @param targetPath 目標(biāo)文件路徑
*/
public static void copyFileByChannelBufferd(String sourcePath, String targetPath) {
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
//獲取通道,StandardOpenOption.READ表示可讀,StandardOpenOption.WRITE表示可寫(xiě),StandardOpenOption.CREATE表示可以創(chuàng)建
inChannel = FileChannel.open(Paths.get(sourcePath), StandardOpenOption.READ);
outChannel = FileChannel.open(Paths.get(targetPath), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE);
//創(chuàng)建內(nèi)存映射文件
MappedByteBuffer inMapped = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer outMapped = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());
//直接操作內(nèi)存映射文件
byte[] buf = new byte[inMapped.limit()];
inMapped.get(buf);
outMapped.put(buf);
} catch (IOException e) {
e.printStackTrace();
} finally {
//關(guān)閉流
try {
if (outChannel != null) {
outChannel.close();
}
if (inChannel != null) {
inChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
7. 通過(guò)JAVA NIO 通道傳輸拷貝文件
方式一
/**
* 通過(guò)JAVA NIO 通道傳輸拷貝文件
*
* @param sourcePath 源文件路徑
* @param targetPath 目標(biāo)文件路徑
*/
public static void copyFileByChannelTransfer(String sourcePath, String targetPath) {
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
//獲取通道
inChannel = FileChannel.open(Paths.get(sourcePath), StandardOpenOption.READ);
outChannel = FileChannel.open(Paths.get(targetPath),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE);
inChannel.transferTo(0,inChannel.size(),outChannel);
} catch (IOException e) {
e.printStackTrace();
}finally {
//關(guān)閉流
try {
if (outChannel != null) {
outChannel.close();
}
if (inChannel != null) {
inChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
方式二
/**
* 通過(guò)JAVA NIO 通道傳輸拷貝文件
*
* @param sourcePath 源文件路徑
* @param targetPath 目標(biāo)文件路徑
*/
public static void copyFileByChannelTransfer2(String sourcePath, String targetPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
fis = new FileInputStream(sourcePath);
fos = new FileOutputStream(targetPath);
//獲取通道
inChannel = fis.getChannel();
outChannel = fos.getChannel();
inChannel.transferTo(0,inChannel.size(),outChannel);
} catch (IOException e) {
e.printStackTrace();
}finally {
//關(guān)閉流
try {
if (outChannel != null) {
outChannel.close();
}
if (inChannel != null) {
inChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用示例
String source = "e:\\demo\\縱天神帝.txt";
String target = "e:\\demo\\";
long time1 = System.currentTimeMillis();
copyFileByStream(source, target + "1.txt");
System.out.println("通過(guò)字節(jié)流實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time1));
long time2 = System.currentTimeMillis();
copyFileByReaderAndWriter(source, target + "2.txt");
System.out.println("通過(guò)字符流實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time2));
long time3 = System.currentTimeMillis();
copyFileByBuffered(source, target + "3.txt");
System.out.println("通過(guò)字節(jié)緩沖流實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time3));
long time4 = System.currentTimeMillis();
copyFileByBufferedChar(source, target + "4.txt");
System.out.println("通過(guò)字符緩沖流實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time4));
long time5 = System.currentTimeMillis();
copyFileByChannel(source, target + "5.txt");
System.out.println("通過(guò)JAVA NIO通道(非直接緩沖區(qū))實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time5));
long time6 = System.currentTimeMillis();
copyFileByChannelBufferd(source, target + "6.txt");
System.out.println("通過(guò)JAVA NIO通道(直接緩沖區(qū))實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time6));
long time7 = System.currentTimeMillis();
copyFileByChannelTransfer(source, target + "7.txt");
System.out.println("通過(guò)JAVA NIO通道傳輸實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time7));
long time8 = System.currentTimeMillis();
copyFileByChannelTransfer(source, target + "8.txt");
System.out.println("通過(guò)JAVA NIO通道傳輸2實(shí)現(xiàn)文件的拷貝耗時(shí):" + (System.currentTimeMillis() - time8));
通過(guò)測(cè)試發(fā)現(xiàn),使用JAVA NIO通道傳輸、JAVA NIO通道直接緩沖區(qū)以及字節(jié)緩沖流拷貝文件效率最高
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于 SpringBoot 實(shí)現(xiàn) MySQL 讀寫(xiě)分離的問(wèn)題
這篇文章主要介紹了基于 SpringBoot 實(shí)現(xiàn) MySQL 讀寫(xiě)分離的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Java Spring Controller 獲取請(qǐng)求參數(shù)的幾種方法詳解
這篇文章主要介紹了Java Spring Controller 獲取請(qǐng)求參數(shù)的幾種方法詳解的相關(guān)資料,這里提供了6種方法,需要的朋友可以參考下2016-12-12
Java實(shí)現(xiàn)的3des加密解密工具類(lèi)示例
這篇文章主要介紹了Java實(shí)現(xiàn)的3des加密解密工具類(lèi),結(jié)合完整實(shí)例形式分析了3des加密解密的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Spring Boot thymeleaf模板引擎的使用詳解
這篇文章主要介紹了Spring Boot thymeleaf模板引擎的使用詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Java網(wǎng)絡(luò)編程之基于TCP協(xié)議
本文主要將Java基于TCP的網(wǎng)絡(luò)編程主要分解成5個(gè)功能:功能分解1:單向通信功能分解,2:雙向通信功能分解,3:對(duì)象流傳送功能分解,4:加入完整的處理異常方式功能分解,5:多線(xiàn)程接收用戶(hù)請(qǐng)求,需要的朋友可以參考下2021-05-05
springboot 多模塊將dao(mybatis)項(xiàng)目拆分出去
這篇文章主要介紹了springboot 多模塊將dao(mybatis)項(xiàng)目拆分出去,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05

