java實(shí)現(xiàn)文件復(fù)制、剪切文件和刪除示例
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Java實(shí)現(xiàn)文件復(fù)制、剪切、刪除操作
* 文件指文件或文件夾
* 文件分割符統(tǒng)一用"\\"
*/
public class FileOperateDemo {
/**
* 復(fù)制文件或文件夾
* @param srcPath 源文件或源文件夾的路徑
* @param destDir 目標(biāo)文件所在的目錄
* @return
*/
public static boolean copyGeneralFile(String srcPath, String destDir) {
boolean flag = false;
File file = new File(srcPath);
if(!file.exists()) { // 源文件或源文件夾不存在
return false;
}
if(file.isFile()) { // 文件復(fù)制
flag = copyFile(srcPath, destDir);
}
else if(file.isDirectory()) { // 文件夾復(fù)制
flag = copyDirectory(srcPath, destDir);
}
return flag;
}
/**
* 默認(rèn)的復(fù)制文件方法,默認(rèn)會(huì)覆蓋目標(biāo)文件夾下的同名文件
* @param srcPath
* 源文件絕對(duì)路徑
* @param destDir
* 目標(biāo)文件所在目錄
* @return boolean
*/
public static boolean copyFile(String srcPath, String destDir) {
return copyFile(srcPath, destDir, true/**overwriteExistFile*/); // 默認(rèn)覆蓋同名文件
}
/**
* 默認(rèn)的復(fù)制文件夾方法,默認(rèn)會(huì)覆蓋目標(biāo)文件夾下的同名文件夾
* @param srcPath 源文件夾路徑
* @param destPath 目標(biāo)文件夾所在目錄
* @return boolean
*/
public static boolean copyDirectory(String srcPath, String destDir) {
return copyDirectory(srcPath, destDir, true/**overwriteExistDir*/);
}
/**
* 復(fù)制文件到目標(biāo)目錄
* @param srcPath
* 源文件絕對(duì)路徑
* @param destDir
* 目標(biāo)文件所在目錄
* @param overwriteExistFile
* 是否覆蓋目標(biāo)目錄下的同名文件
* @return boolean
*/
public static boolean copyFile(String srcPath, String destDir, boolean overwriteExistFile) {
boolean flag = false;
File srcFile = new File(srcPath);
if (!srcFile.exists() || !srcFile.isFile()) { // 源文件不存在
return false;
}
//獲取待復(fù)制文件的文件名
String fileName = srcFile.getName();
String destPath = destDir + File.separator +fileName;
File destFile = new File(destPath);
if (destFile.getAbsolutePath().equals(srcFile.getAbsolutePath())) { // 源文件路徑和目標(biāo)文件路徑重復(fù)
return false;
}
if(destFile.exists() && !overwriteExistFile) { // 目標(biāo)目錄下已有同名文件且不允許覆蓋
return false;
}
File destFileDir = new File(destDir);
if(!destFileDir.exists() && !destFileDir.mkdirs()) { // 目錄不存在并且創(chuàng)建目錄失敗直接返回
return false;
}
try {
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] buf = new byte[1024];
int c;
while ((c = fis.read(buf)) != -1) {
fos.write(buf, 0, c);
}
fos.flush();
fis.close();
fos.close();
flag = true;
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
/**
*
* @param srcPath 源文件夾路徑
* @param destPath 目標(biāo)文件夾所在目錄
* @return
*/
public static boolean copyDirectory(String srcPath, String destDir, boolean overwriteExistDir) {
if(destDir.contains(srcPath))
return false;
boolean flag = false;
File srcFile = new File(srcPath);
if (!srcFile.exists() || !srcFile.isDirectory()) { // 源文件夾不存在
return false;
}
//獲得待復(fù)制的文件夾的名字,比如待復(fù)制的文件夾為"E:\\dir\\"則獲取的名字為"dir"
String dirName = srcFile.getName();
//目標(biāo)文件夾的完整路徑
String destDirPath = destDir + File.separator + dirName + File.separator;
File destDirFile = new File(destDirPath);
if(destDirFile.getAbsolutePath().equals(srcFile.getAbsolutePath())) {
return false;
}
if(destDirFile.exists() && destDirFile.isDirectory() && !overwriteExistDir) { // 目標(biāo)位置有一個(gè)同名文件夾且不允許覆蓋同名文件夾,則直接返回false
return false;
}
if(!destDirFile.exists() && !destDirFile.mkdirs()) { // 如果目標(biāo)目錄不存在并且創(chuàng)建目錄失敗
return false;
}
File[] fileList = srcFile.listFiles(); //獲取源文件夾下的子文件和子文件夾
if(fileList.length==0) { // 如果源文件夾為空目錄則直接設(shè)置flag為true,這一步非常隱蔽,debug了很久
flag = true;
}
else {
for(File temp: fileList) {
if(temp.isFile()) { // 文件
flag = copyFile(temp.getAbsolutePath(), destDirPath, overwriteExistDir); // 遞歸復(fù)制時(shí)也繼承覆蓋屬性
}
else if(temp.isDirectory()) { // 文件夾
flag = copyDirectory(temp.getAbsolutePath(), destDirPath, overwriteExistDir); // 遞歸復(fù)制時(shí)也繼承覆蓋屬性
}
if(!flag) {
break;
}
}
}
return flag;
}
/**
* 刪除文件或文件夾
* @param path
* 待刪除的文件的絕對(duì)路徑
* @return boolean
*/
public static boolean deleteFile(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) { // 文件不存在直接返回
return flag;
}
flag = file.delete();
return flag;
}
/**
* 由上面方法延伸出剪切方法:復(fù)制+刪除
* @param destDir 同上
*/
public static boolean cutGeneralFile(String srcPath, String destDir) {
boolean flag = false;
if(copyGeneralFile(srcPath, destDir) && deleteFile(srcPath)) { // 復(fù)制和刪除都成功
flag = true;
}
return flag;
}
public static void main(String[] args) {
/** 測(cè)試復(fù)制文件 */
System.out.println(copyGeneralFile("d://test/test.html", "d://test/test/")); // 一般正常場(chǎng)景
System.out.println(copyGeneralFile("d://notexistfile", "d://test/")); // 復(fù)制不存在的文件或文件夾
System.out.println(copyGeneralFile("d://test/test.html", "d://test/")); // 待復(fù)制文件與目標(biāo)文件在同一目錄下
System.out.println(copyGeneralFile("d://test/test.html", "d://test/test/")); // 覆蓋目標(biāo)目錄下的同名文件
System.out.println(copyFile("d://test/", "d://test2", false)); // 不覆蓋目標(biāo)目錄下的同名文件
System.out.println(copyGeneralFile("d://test/test.html", "notexist://noexistdir/")); // 復(fù)制文件到一個(gè)不可能存在也不可能創(chuàng)建的目錄下
System.out.println("---------");
/** 測(cè)試復(fù)制文件夾 */
System.out.println(copyGeneralFile("d://test/", "d://test2/"));
System.out.println("---------");
/** 測(cè)試刪除文件 */
System.out.println(deleteFile("d://a/"));
}
}
相關(guān)文章
使用IDEA創(chuàng)建java項(xiàng)目的步驟詳解(hello word)
這篇文章主要介紹了使用IDEA創(chuàng)建java項(xiàng)目的步驟詳解(hello word),本文分步驟通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12解決常見的Eclipse SVN插件報(bào)錯(cuò)方法詳解
本篇文章是對(duì)常見的Eclipse SVN插件報(bào)錯(cuò)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05使用Spring的JAVA Mail支持簡(jiǎn)化郵件發(fā)送功能
這篇文章主要為大家詳細(xì)介紹了使用Spring的JAVA Mail支持簡(jiǎn)化郵件發(fā)送功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04超全MyBatis動(dòng)態(tài)代理詳解(絕對(duì)干貨)
這篇文章主要介紹了超全MyBatis動(dòng)態(tài)代理詳解(絕對(duì)干貨),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Java開發(fā)中的23種設(shè)計(jì)模式詳解(推薦)
本篇文章主要介紹了Java開發(fā)中的23種設(shè)計(jì)模式詳解,現(xiàn)在分享給大家,也給大家做個(gè)參考。感興趣的小伙伴們可以參考一下。 設(shè)計(jì)模式(Design Patterns)2016-11-11java 動(dòng)態(tài)加載的實(shí)現(xiàn)代碼
這篇文章主要介紹了java 動(dòng)態(tài)加載的實(shí)現(xiàn)代碼的相關(guān)資料,Java動(dòng)態(tài)加載類主要是為了不改變主程序代碼,通過(guò)修改配置文件就可以操作不同的對(duì)象執(zhí)行不同的功能,需要的朋友可以參考下2017-07-07