Posted on 2012-04-15 16:37
zljpp 閱讀(250)
評論(0) 編輯 收藏
package com.file;
import java.io.File;//引入類
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
//實現文件的簡單處理,復制和移動文件、目錄等
public class TextCopyFileAndMove {
public static void fileMove(String from, String to) throws Exception {// 移動指定文件夾內的全部文件
try {
File dir = new File(from);
File[] files = dir.listFiles();// 將文件或文件夾放入文件集
if (files == null)// 判斷文件集是否為空
return;
File moveDir = new File(to);// 創建目標目錄
if (!moveDir.exists()) {// 判斷目標目錄是否存在
moveDir.mkdirs();// 不存在則創建
}
for (int i = 0; i < files.length; i++) {// 遍歷文件集
if (files[i].isDirectory()) {// 如果是文件夾或目錄,則遞歸調用fileMove方法,直到獲得目錄下的文件
fileMove(files[i].getPath(), to + "\\" + files[i].getName());// 遞歸移動文件
files[i].delete();// 刪除文件所在原目錄
}
File moveFile = new File(moveDir.getPath() + "\\"http:// 將文件目錄放入移動后的目錄
+ files[i].getName());
if (moveFile.exists()) {// 目標文件夾下存在的話,刪除
moveFile.delete();
}
files[i].renameTo(moveFile);// 移動文件
System.out.println(files[i] + " 移動成功");
}
} catch (Exception e) {
throw e;
}
}
// 復制目錄下的文件(不包括該目錄)到指定目錄,會連同子目錄一起復制過去。
public static void copyFileFromDir(String toPath, String fromPath) {
File file = new File(fromPath);
createFile(toPath, false);// true:創建文件 false創建目錄
if (file.isDirectory()) {// 如果是目錄
copyFileToDir(toPath, listFile(file));
}
}
// 復制目錄到指定目錄,將目錄以及目錄下的文件和子目錄全部復制到目標目錄
public static void copyDir(String toPath, String fromPath) {
File targetFile = new File(toPath);// 創建文件
createFile(targetFile, false);// 創建目錄
File file = new File(fromPath);// 創建文件
if (targetFile.isDirectory() && file.isDirectory()) {// 如果傳入是目錄
copyFileToDir(targetFile.getAbsolutePath() + "/" + file.getName(),
listFile(file));// 復制文件到指定目錄
}
}
// 復制一組文件到指定目錄。targetDir是目標目錄,filePath是需要復制的文件路徑
public static void copyFileToDir(String toDir, String[] filePath) {
if (toDir == null || "".equals(toDir)) {// 目錄路徑為空
System.out.println("參數錯誤,目標路徑不能為空");
return;
}
File targetFile = new File(toDir);
if (!targetFile.exists()) {// 如果指定目錄不存在
targetFile.mkdir();// 新建目錄
} else {
if (!targetFile.isDirectory()) {// 如果不是目錄
System.out.println("參數錯誤,目標路徑指向的不是一個目錄!");
return;
}
}
for (int i = 0; i < filePath.length; i++) {// 遍歷需要復制的文件路徑
File file = new File(filePath[i]);// 創建文件
if (file.isDirectory()) {// 判斷是否是目錄
copyFileToDir(toDir + "/" + file.getName(), listFile(file));// 遞歸調用方法獲得目錄下的文件
System.out.println("復制文件 " + file);
} else {
copyFileToDir(toDir, file, "");// 復制文件到指定目錄
}
}
}
public static void copyFileToDir(String toDir, File file, String newName) {// 復制文件到指定目錄
String newFile = "";
if (newName != null && !"".equals(newName)) {
newFile = toDir + "/" + newName;
} else {
newFile = toDir + "/" + file.getName();
}
File tFile = new File(newFile);
copyFile(tFile, file);// 調用方法復制文件
}
public static void copyFile(File toFile, File fromFile) {// 復制文件
if (toFile.exists()) {// 判斷目標目錄中文件是否存在
System.out.println("文件" + toFile.getAbsolutePath() + "已經存在,跳過該文件!");
return;
} else {
createFile(toFile, true);// 創建文件
}
System.out.println("復制文件" + fromFile.getAbsolutePath() + "到"
+ toFile.getAbsolutePath());
try {
InputStream is = new FileInputStream(fromFile);// 創建文件輸入流
FileOutputStream fos = new FileOutputStream(toFile);// 文件輸出流
byte[] buffer = new byte[1024];// 字節數組
while (is.read(buffer) != -1) {// 將文件內容寫到文件中
fos.write(buffer);
}
is.close();// 輸入流關閉
fos.close();// 輸出流關閉
} catch (FileNotFoundException e) {// 捕獲文件不存在異常
e.printStackTrace();
} catch (IOException e) {// 捕獲異常
e.printStackTrace();
}
}
public static String[] listFile(File dir) {// 獲取文件絕對路徑
String absolutPath = dir.getAbsolutePath();// 聲獲字符串賦值為路傳入文件的路徑
String[] paths = dir.list();// 文件名數組
String[] files = new String[paths.length];// 聲明字符串數組,長度為傳入文件的個數
for (int i = 0; i < paths.length; i++) {// 遍歷顯示文件絕對路徑
files[i] = absolutPath + "/" + paths[i];
}
return files;
}
public static void createFile(String path, boolean isFile) {// 創建文件或目錄
createFile(new File(path), isFile);// 調用方法創建新文件或目錄
}
public static void createFile(File file, boolean isFile) {// 創建文件
if (!file.exists()) {// 如果文件不存在
if (!file.getParentFile().exists()) {// 如果文件父目錄不存在
createFile(file.getParentFile(), false);
} else {// 存在文件父目錄
if (isFile) {// 創建文件
try {
file.createNewFile();// 創建新文件
} catch (IOException e) {
e.printStackTrace();
}
} else {
file.mkdir();// 創建目錄
}
}
}
}
public static void main(String[] args) {// java程序主入口處
String fromPath = "E:/createFile";// 目錄路徑
String toPath = "F:/createFile";// 源路徑
System.out.println("1.移動文件:從路徑 " + fromPath + " 移動到路徑 " + toPath);
try {
fileMove(fromPath, toPath);// 調用方法實現文件的移動
} catch (Exception e) {
System.out.println("移動文件出現問題" + e.getMessage());
}
System.out.println("2.復制目錄 " + toPath + " 下的文件(不包括該目錄)到指定目錄" + fromPath
+ " ,會連同子目錄一起復制過去。");
copyFileFromDir(fromPath, toPath);// 調用方法實現目錄復制
System.out.println("3.復制目錄 " + fromPath + "到指定目錄 " + toPath
+ " ,將目錄以及目錄下的文件和子目錄全部復制到目標目錄");
copyDir(toPath, fromPath);// 調用方法實現目錄以用目錄下的文件和子目錄全部復制
}
}