文件和文件夾操作(複製,剪切,刪除)

 

文件和文件夾操作(複製,剪切,刪除)

window 系統使用(已經經過測試):

 

package com.funo.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * <pre>
 * &lt;b&gt;&lt;font color=&quot;blue&quot;&gt;FileOperate&lt;/font&gt;&lt;/b&gt;
 * </pre>
 *
 * <pre>
 * &lt;b&gt; --描述說明--&lt;/b&gt;
 * Java實現文件複製、剪切、刪除操作
 * 文件指文件或文件夾
 * 文件分割符統一用&quot;//&quot;
 * </pre>
 * <pre></pre>
 * <pre>
 * &lt;b&gt;--樣例--&lt;/b&gt;
 *   FileOperate obj = new FileOperate();
 *   obj.method();
 * </pre>
 *
 * JDK版本:JDK1.5
 *
 * @author <b>林祥仙</b>
 */
public class FileOperate {

 /**
  * 複製文件或文件夾
  *
  * @param srcPath
  * @param destDir
  *            目標文件所在的目錄
  * @return
  */
 public static boolean copyGeneralFile(String srcPath, String destDir) {
  boolean flag = false;
  File file = new File(srcPath);
  if (!file.exists()) {
   System.out.println("源文件或源文件夾不存在!");
   return false;
  }
  if (file.isFile()) { // 源文件
   System.out.println("下面進行文件複製!");
   flag = copyFile(srcPath, destDir);
  } else if (file.isDirectory()) {
   System.out.println("下面進行文件夾複製!");
   flag = copyDirectory(srcPath, destDir);
  }

  return flag;
 }

 /**
  * 複製文件
  *
  * @param srcPath
  *            源文件絕對路徑
  * @param destDir
  *            目標文件所在目錄
  * @return boolean
  */
 private static boolean copyFile(String srcPath, String destDir) {
  boolean flag = false;
  if (srcPath.indexOf("\\") != -1)
   srcPath = srcPath.replaceAll("\\\\", "//");
  File srcFile = new File(srcPath);
  if (!srcFile.exists()) { // 源文件不存在
   System.out.println("源文件不存在");
   return false;
  }
  // 獲取待複製文件的文件名
  String fileName = srcPath.substring(srcPath.lastIndexOf("//"));
  // String fileName = srcPath.substring(srcPath.lastIndexOf("//"));
  String destPath = destDir + fileName;
  if (destPath.equals(srcPath)) { // 源文件路徑和目標文件路徑重複
   System.out.println("源文件路徑和目標文件路徑重複!");
   return false;
  }
  File destFile = new File(destPath);
  if (destFile.exists() && destFile.isFile()) { // 該路徑下已經有一個同名文件
   System.out.println("目標目錄下已有同名文件!");
   return false;
  }

  File destFileDir = new File(destDir);
  destFileDir.mkdirs();
  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);
   }
   fis.close();
   fos.close();

   flag = true;
  } catch (IOException e) {
   //
  }

  if (flag) {
   System.out.println("複製文件成功!");
  }

  return flag;
 }

 /**
  *
  * @param srcPath
  *            源文件夾路徑
  * @param destPath
  *            目標文件夾所在目錄
  * @return
  */
 private static boolean copyDirectory(String srcPath, String destDir) {
  System.out.println("複製文件夾開始!");
  boolean flag = false;
  if (srcPath.indexOf("\\") != -1)
   srcPath = srcPath.replaceAll("\\\\", "//");

  File srcFile = new File(srcPath);
  if (!srcFile.exists()) { // 源文件夾不存在
   System.out.println("源文件夾不存在");
   return false;
  }
  // 獲得待複製的文件夾的名字,比如待複製的文件夾爲"E://dir"則獲取的名字爲"dir"
  String dirName = getDirName(srcPath);
  dirName = srcPath.substring(dirName.lastIndexOf("//"));
  // 目標文件夾的完整路徑
  // String destPath = destDir;
  String destPath = destDir + dirName;
  // System.out.println("目標文件夾的完整路徑爲:" + destPath);

  if (destPath.equals(srcPath)) {
   System.out.println("目標文件夾與源文件夾重複");
   return false;
  }
  File destDirFile = new File(destPath);
  if (destDirFile.exists()) { // 目標位置有一個同名文件夾
   System.out.println("目標位置已有同名文件夾!");
   return false;
  }
  destDirFile.mkdirs(); // 生成目錄

  File[] fileList = srcFile.listFiles(); // 獲取源文件夾下的子文件和子文件夾
  if (fileList.length == 0) { // 如果源文件夾爲空目錄則直接設置flag爲true,這一步非常隱蔽,debug了很久
   flag = true;
  } else {
   for (File temp : fileList) {
    if (temp.isFile()) { // 文件
     flag = copyFile(temp.getAbsolutePath(), destPath);
    } else if (temp.isDirectory()) { // 文件夾
     flag = copyDirectory(temp.getAbsolutePath(), destPath);
    }
    if (!flag) {
     break;
    }
   }
  }

  if (flag) {
   System.out.println("複製文件夾成功!");
  }

  return flag;
 }

 /**
  * 獲取待複製文件夾的文件夾名
  *
  * @param dir
  * @return String
  */
 private static String getDirName(String dir) {
  if (dir.endsWith(File.separator)) { // 如果文件夾路徑以"//"結尾,則先去除末尾的"//"
   dir = dir.substring(0, dir.lastIndexOf(File.separator));
  }
  return dir.substring(dir.lastIndexOf(File.separator) + 1);
 }

 /**
  * 刪除文件或文件夾
  *
  * @param path
  *            待刪除的文件的絕對路徑
  * @return boolean
  */
 public static boolean deleteGeneralFile(String path) {
  boolean flag = false;

  File file = new File(path);
  if (!file.exists()) { // 文件不存在
   System.out.println("要刪除的文件不存在!");
  }

  if (file.isDirectory()) { // 如果是目錄,則單獨處理
   flag = deleteDirectory(file.getAbsolutePath());
  } else if (file.isFile()) {
   flag = deleteFile(file);
  }

  if (flag) {
   System.out.println("刪除文件或文件夾成功!");
  }

  return flag;
 }

 /**
  * 刪除文件
  *
  * @param file
  * @return boolean
  */
 private static boolean deleteFile(File file) {
  return file.delete();
 }

 /**
  * 刪除目錄及其下面的所有子文件和子文件夾,注意一個目錄下如果還有其他文件或文件夾
  * 則直接調用delete方法是不行的,必須待其子文件和子文件夾完全刪除了才能夠調用delete
  *
  * @param path
  *            path爲該目錄的路徑
  */
 private static boolean deleteDirectory(String path) {
  boolean flag = true;
  File dirFile = new File(path);
  if (!dirFile.isDirectory()) {
   return flag;
  }
  File[] files = dirFile.listFiles();
  for (File file : files) { // 刪除該文件夾下的文件和文件夾
   // Delete file.
   if (file.isFile()) {
    flag = deleteFile(file);
   } else if (file.isDirectory()) {// Delete folder
    flag = deleteDirectory(file.getAbsolutePath());
   }
   if (!flag) { // 只要有一個失敗就立刻不再繼續
    break;
   }
  }
  flag = dirFile.delete(); // 刪除空目錄
  return flag;
 }

 /**
  * 由上面方法延伸出剪切方法:複製+刪除
  *
  * @param destDir
  *            同上
  */
 public static boolean cutGeneralFile(String srcPath, String destDir) {
  if (!copyGeneralFile(srcPath, destDir)) {
   System.out.println("複製失敗導致剪切失敗!");
   return false;
  }
  if (!deleteGeneralFile(srcPath)) {
   System.out.println("刪除源文件(文件夾)失敗導致剪切失敗!");
   return false;
  }

  System.out.println("剪切成功!");
  return true;
 }

 public static void main(String[] args) {
   String fgf = File.separator;
//   copyGeneralFile("E://tmp//data//192.168.1.201_8081_access_log_2011-11-15.txt",
//   "E://tmp//backup"); // 複製文件
//   copyGeneralFile("E://tmp", "E://tmp3"); // 複製文件夾
//   deleteGeneralFile("E://tmp//backup//192.168.1.201_8081_access_log_2011-11-15.txt"); // 刪除文件
//   deleteGeneralFile("E://tmp//tmp"); // 刪除文件夾
         cutGeneralFile("E://tmp//data", "E://tmp//tmpbackup"); // 剪切文件夾 實際目標文件夾路徑: E:\tmp\tmp\data
//   cutGeneralFile("E://tmp//tmp//data//192.168.1.201_8081_access_log_2011-11-15.txt",
//   "E://tmp//backup"); // 剪切文件
 }

}

 

 

unix 系統使用,但是我沒測試過;

package FileOperation;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
* Java實現文件複製、剪切、刪除操作
* 文件指文件或文件夾
* 文件分割符統一用"//"
*/

public class FileOperateDemo {

/**
* 複製文件或文件夾
* @param srcPath
* @param destDir 目標文件所在的目錄
* @return
*/
public static boolean copyGeneralFile(String srcPath, String destDir) {
boolean flag = false;
File file = new File(srcPath);
if(!file.exists()) {
System.out.println("源文件或源文件夾不存在!");
return false;
}
if(file.isFile()) { //源文件
System.out.println("下面進行文件複製!");
flag = copyFile(srcPath, destDir);
}
else if(file.isDirectory()) {
System.out.println("下面進行文件夾複製!");
flag = copyDirectory(srcPath, destDir);
}

return flag;
}

/**
* 複製文件
*
* @param srcPath
* 源文件絕對路徑
* @param destDir
* 目標文件所在目錄
* @return boolean
*/
private static boolean copyFile(String srcPath, String destDir) {
boolean flag = false;

File srcFile = new File(srcPath);
if (!srcFile.exists()) { // 源文件不存在
System.out.println("源文件不存在");
return false;
}
//獲取待複製文件的文件名
String fileName = srcPath.substring(srcPath.lastIndexOf(File.separator));
String destPath = destDir + fileName;
if (destPath.equals(srcPath)) { // 源文件路徑和目標文件路徑重複
System.out.println("源文件路徑和目標文件路徑重複!");
return false;
}
File destFile = new File(destPath);
if(destFile.exists() && destFile.isFile()) { //該路徑下已經有一個同名文件
System.out.println("目標目錄下已有同名文件!");
return false;
}

File destFileDir = new File(destDir);
destFileDir.mkdirs();
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);
}
fis.close();
fos.close();

flag = true;
} catch (IOException e) {
//
}

if(flag) {
System.out.println("複製文件成功!");
}

return flag;
}

/**
*
* @param srcPath 源文件夾路徑
* @param destPath 目標文件夾所在目錄
* @return
*/
private static boolean copyDirectory(String srcPath, String destDir) {
System.out.println("複製文件夾開始!");
boolean flag = false;

File srcFile = new File(srcPath);
if (!srcFile.exists()) { // 源文件夾不存在
System.out.println("源文件夾不存在");
return false;
}
//獲得待複製的文件夾的名字,比如待複製的文件夾爲"E://dir"則獲取的名字爲"dir"
String dirName = getDirName(srcPath);
//目標文件夾的完整路徑
String destPath = destDir + File.separator + dirName;
// System.out.println("目標文件夾的完整路徑爲:" + destPath);

if(destPath.equals(srcPath)) {
System.out.println("目標文件夾與源文件夾重複");
return false;
}
File destDirFile = new File(destPath);
if(destDirFile.exists()) { //目標位置有一個同名文件夾
System.out.println("目標位置已有同名文件夾!");
return false;
}
destDirFile.mkdirs(); //生成目錄

File[] fileList = srcFile.listFiles(); //獲取源文件夾下的子文件和子文件夾
if(fileList.length==0) { //如果源文件夾爲空目錄則直接設置flag爲true,這一步非常隱蔽,debug了很久
flag = true;
}
else {
for(File temp: fileList) {
if(temp.isFile()) { //文件
flag = copyFile(temp.getAbsolutePath(), destPath);
}
else if(temp.isDirectory()) { //文件夾
flag = copyDirectory(temp.getAbsolutePath(), destPath);
}
if(!flag) {
break;
}
}
}

if(flag) {
System.out.println("複製文件夾成功!");
}

return flag;
}

/**
* 獲取待複製文件夾的文件夾名
* @param dir
* @return String
*/
private static String getDirName(String dir) {
if(dir.endsWith(File.separator)) { //如果文件夾路徑以"//"結尾,則先去除末尾的"//"
dir = dir.substring(0, dir.lastIndexOf(File.separator));
}
return dir.substring(dir.lastIndexOf(File.separator)+1);
}

/**
* 刪除文件或文件夾
*
* @param path
* 待刪除的文件的絕對路徑
* @return boolean
*/
public static boolean deleteGeneralFile(String path) {
boolean flag = false;

File file = new File(path);
if (!file.exists()) { // 文件不存在
System.out.println("要刪除的文件不存在!");
}

if (file.isDirectory()) { // 如果是目錄,則單獨處理
flag = deleteDirectory(file.getAbsolutePath());
} else if (file.isFile()) {
flag = deleteFile(file);
}

if (flag) {
System.out.println("刪除文件或文件夾成功!");
}

return flag;
}

/**
* 刪除文件
* @param file
* @return boolean
*/
private static boolean deleteFile(File file) {
return file.delete();
}

/**
* 刪除目錄及其下面的所有子文件和子文件夾,注意一個目錄下如果還有其他文件或文件夾
* 則直接調用delete方法是不行的,必須待其子文件和子文件夾完全刪除了才能夠調用delete
* @param path
* path爲該目錄的路徑
*/
private static boolean deleteDirectory(String path) {
boolean flag = true;
File dirFile = new File(path);
if (!dirFile.isDirectory()) {
return flag;
}
File[] files = dirFile.listFiles();
for (File file : files) { // 刪除該文件夾下的文件和文件夾
// Delete file.
if (file.isFile()) {
flag = deleteFile(file);
} else if (file.isDirectory()) {// Delete folder
flag = deleteDirectory(file.getAbsolutePath());
}
if (!flag) { // 只要有一個失敗就立刻不再繼續
break;
}
}
flag = dirFile.delete(); // 刪除空目錄
return flag;
}

/**
* 由上面方法延伸出剪切方法:複製+刪除
* @param destDir 同上
*/
public static boolean cutGeneralFile(String srcPath, String destDir) {
if(!copyGeneralFile(srcPath, destDir)) {
System.out.println("複製失敗導致剪切失敗!");
return false;
}
if(!deleteGeneralFile(srcPath)) {
System.out.println("刪除源文件(文件夾)失敗導致剪切失敗!");
return false;
}

System.out.println("剪切成功!");
return true;
}

public static void main(String[] args) {
copyGeneralFile("E://Assemble.txt", "E://New.txt"); //複製文件
copyGeneralFile("E://hello", "E://world"); //複製文件夾
deleteGeneralFile("E://onlinestockdb.sql"); //刪除文件
deleteGeneralFile("E://woman"); //刪除文件夾
cutGeneralFile("E://hello", "E://world"); //剪切文件夾
cutGeneralFile("E://Difficult.java", "E://Cow//"); //剪切文件
}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章