標籤:rom except bool extc stack while ++ throw 絕對路徑
package com.sitech.message.controller.task;
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() + "\\"// 將檔案目錄放入移動後的目錄
+ 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/shafei.log";// 目錄路徑
String toPath = "F:/createFile/shafei.log";// 源路徑
System.out.println("1.移動檔案:從路徑 " + fromPath + " 移動到路徑 " + toPath);
File fromPaths = new File("F:/createFile/createFile/test.log");
File tofiles = new File("F:/createFile/shafei/test.log");
copyFile(tofiles,fromPaths);
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);// 調用方法實現目錄以用目錄下的檔案和子目錄全部複製
}
}
java檔案上傳複製等功能