1. 程式人生 > >操作文件和文件夾

操作文件和文件夾

folders filename 遍歷 文件路徑 rac stack h+ 復制 not found

package Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

/**
 * 功能:該類用於操作文件和文件夾
 */
public class FileUtil {
    
    /**
     * 將源文件的數據寫入到目標文件中,
     * 不會檢查源文件是否存在,
     * 若目標文件存在則直接寫入,
     * 否則創建目標文件後再進行寫入。
     * @param srcPath
     * @param desPath
     
*/ private static void copyFile(String srcPath,String desPath){ try { FileInputStream in = new FileInputStream(srcPath); FileOutputStream out = new FileOutputStream(desPath); byte[] bt = new byte[1024]; int count; while ((count = in
.read(bt)) > 0) { out.write(bt, 0, count); } in.close(); out.close(); } catch (IOException ex) { ex.printStackTrace(); } } /** * 復制文件,若文件存在則替換該文件。 * @param srcPath * @param desPath * @throws Exception
*/ public static void copyAndReplaceFile(String srcPath,String desPath) throws Exception{ srcPath = separatorReplace(srcPath); desPath = separatorReplace(desPath); File srcFile = new File(srcPath); if(!srcFile.isFile()){ throw new Exception("source file not found!"); } copyFile(srcPath,desPath); } /** * 復制文件,若文件已存在則不進行替換。 * @param srcPath * @param desPath * @throws Exception */ public static void copyAndNotReplaceFile(String srcPath,String desPath) throws Exception{ srcPath = separatorReplace(srcPath); desPath = separatorReplace(desPath); File srcFile = new File(srcPath); File desFile = new File(desPath); if(!srcFile.isFile()){ throw new Exception("source file not found!"); } if(desFile.isFile()){ return; } copyFile(srcPath,desPath); } /** * 移動文件,若文件存在則替換該文件。 * @param srcPath * @param desPath * @throws Exception */ public static void moveAndReplaceFile(String srcPath,String desPath) throws Exception{ srcPath = separatorReplace(srcPath); desPath = separatorReplace(desPath); copyAndReplaceFile(srcPath,desPath); deleteFile(srcPath); } /** * 移動文件,若文件存在則不進行替換。 * @param srcPath * @param desPath * @throws Exception */ public static void moveAndNotReplaceFile(String srcPath,String desPath) throws Exception{ srcPath = separatorReplace(srcPath); desPath = separatorReplace(desPath); copyAndNotReplaceFile(srcPath,desPath); deleteFile(srcPath); } /** * 復制並合並文件夾, * 不會替換目標文件夾中已經存在的文件或文件夾。 * @param srcPath * @param desPath * @throws Exception */ public static void copyAndMergerFolder(String srcPath,String desPath) throws Exception{ srcPath = separatorReplace(srcPath); desPath = separatorReplace(desPath); File folder = getFolder(srcPath); createFolder(desPath); File[] files = folder.listFiles(); for(File file:files){ String src = file.getAbsolutePath(); String des = desPath+File.separator+file.getName(); if(file.isFile()){ copyAndNotReplaceFile(src,des); }else if(file.isDirectory()){ copyAndMergerFolder(src,des); } } } /** * 復制並替換文件夾, * 將目標文件夾完全替換成源文件夾, * 目標文件夾原有的資料會丟失。 * @param srcPath * @param desPath * @throws Exception */ public static void copyAndReplaceFolder(String srcPath,String desPath) throws Exception{ srcPath = separatorReplace(srcPath); desPath = separatorReplace(desPath); File folder = getFolder(srcPath); createNewFolder(desPath); File[] files = folder.listFiles(); for(File file:files){ String src = file.getAbsolutePath(); String des = desPath+File.separator+file.getName(); if(file.isFile()){ copyAndReplaceFile(src,des); }else if(file.isDirectory()){ copyAndReplaceFolder(src,des); } } } /** * 合並文件夾後,將源文件夾刪除。 * @param srcPath * @param desPath * @throws Exception */ public static void moveAndMergerFolder(String srcPath,String desPath) throws Exception{ srcPath = separatorReplace(srcPath); desPath = separatorReplace(desPath); copyAndMergerFolder(srcPath,desPath); deleteFolder(srcPath); } /** * 替換文件夾後,將源文件夾刪除。 * @param srcPath * @param desPath * @throws Exception */ public static void moveAndReplaceFolder(String srcPath,String desPath) throws Exception{ srcPath = separatorReplace(srcPath); desPath = separatorReplace(desPath); copyAndReplaceFolder(srcPath,desPath); deleteFolder(srcPath); } /** * 創建文件夾,如果文件夾存在則不進行創建。 * @param path * @throws Exception */ public static void createFolder(String path) throws Exception{ path = separatorReplace(path); File folder = new File(path); if(folder.isDirectory()){ return; }else if(folder.isFile()){ deleteFile(path); } folder.mkdirs(); } /** * 創建一個新的文件夾,如果文件夾存在,則刪除後再創建。 * @param path * @throws Exception */ public static void createNewFolder(String path) throws Exception{ path = separatorReplace(path); File folder = new File(path); if(folder.isDirectory()){ deleteFolder(path); }else if(folder.isFile()){ deleteFile(path); } folder.mkdirs(); } /** * 創建一個文件,如果文件存在則不進行創建。 * @param path * @throws Exception */ public static File createFile(String path) throws Exception{ path = separatorReplace(path); File file = new File(path); if(file.isFile()){ return file; }else if(file.isDirectory()){ deleteFolder(path); } return createFile(file); } /** * 創建一個新文件,如果存在同名的文件或文件夾將會刪除該文件或文件夾, * 如果父目錄不存在則創建父目錄。 * @param path * @throws Exception */ public static File createNewFile(String path) throws Exception{ path = separatorReplace(path); File file = new File(path); if(file.isFile()){ deleteFile(path); }else if(file.isDirectory()){ deleteFolder(path); } return createFile(file); } /** * 分隔符替換 * window下測試通過 * @param path * @return */ public static String separatorReplace(String path){ return path.replace("\\","/"); } /** * 創建文件及其父目錄。 * @param file * @throws Exception */ public static File createFile(File file) throws Exception{ createParentFolder(file); if(!file.createNewFile()) { throw new Exception("create file failure!"); } return file; } /** * 創建父目錄 * @param file * @throws Exception */ private static void createParentFolder(File file) throws Exception{ if(!file.getParentFile().exists()) { if(!file.getParentFile().mkdirs()) { throw new Exception("create parent directory failure!"); } } } /** * 根據文件路徑刪除文件,如果路徑指向的文件不存在或刪除失敗則拋出異常。 * @param path * @return * @throws Exception */ public static void deleteFile(String path) throws Exception { path = separatorReplace(path); File file = getFile(path); if(!file.delete()){ throw new Exception("delete file failure"); } } /** * 刪除指定目錄中指定前綴和後綴的文件。 * @param dir * @param prefix * @param suffix * @throws Exception */ public static void deleteFile(String dir,String prefix,String suffix) throws Exception{ dir = separatorReplace(dir); File directory = getFolder(dir); File[] files = directory.listFiles(); for(File file:files){ if(file.isFile()){ String fileName = file.getName(); if(fileName.startsWith(prefix)&&fileName.endsWith(suffix)){ deleteFile(file.getAbsolutePath()); } } } } /** * 根據路徑刪除文件夾,如果路徑指向的目錄不存在則拋出異常, * 若存在則先遍歷刪除子項目後再刪除文件夾本身。 * @param path * @throws Exception */ public static void deleteFolder(String path) throws Exception { path = separatorReplace(path); File folder = getFolder(path); File[] files = folder.listFiles(); for(File file:files) { if(file.isDirectory()){ deleteFolder(file.getAbsolutePath()); }else if(file.isFile()){ deleteFile(file.getAbsolutePath()); } } folder.delete(); } /** * 查找目標文件夾下的目標文件 * @param dir * @param fileName * @return * @throws FileNotFoundException */ public static File searchFile(String dir,String fileName) throws FileNotFoundException{ dir = separatorReplace(dir); File f = null; File folder = getFolder(dir); File[] files = folder.listFiles(); for(File file:files) { if(file.isDirectory()){ f = searchFile(file.getAbsolutePath(),fileName); if(f!=null){ break; } }else if(file.isFile()){ if(file.getName().equals(fileName)){ f = file; break; } } } return f; } /** * 獲得文件類型。 * @param path:文件路徑 * @return * @throws FileNotFoundException */ public static String getFileType(String path) throws FileNotFoundException { path = separatorReplace(path); File file = getFile(path); String fileName = file.getName(); String[] strs = fileName.split("\\."); if(strs.length<2){ return "unknownType"; } return strs[strs.length-1]; } /** * 根據文件路徑,獲得該路徑指向的文件的大小。 * @param path * @return * @throws FileNotFoundException */ public static long getFileSize(String path) throws FileNotFoundException{ path = separatorReplace(path); File file = getFile(path); return file.length(); } /** * 根據文件夾路徑,獲得該路徑指向的文件夾的大小。 * 遍歷該文件夾及其子目錄的文件,將這些文件的大小進行累加,得出的就是文件夾的大小。 * @param path * @return * @throws FileNotFoundException */ public static long getFolderSize(String path) throws FileNotFoundException{ path = separatorReplace(path); long size = 0; File folder = getFolder(path); File[] files = folder.listFiles(); for(File file:files){ if(file.isDirectory()){ size += getFolderSize(file.getAbsolutePath()); }else if(file.isFile()){ size += file.length(); } } return size; } /** * 通過路徑獲得文件, * 若不存在則拋異常, * 若存在則返回該文件。 * @param path * @return * @throws FileNotFoundException */ public static File getFile(String path) throws FileNotFoundException{ path = separatorReplace(path); File file = new File(path); if(!file.isFile()){ throw new FileNotFoundException("file not found!"); } return file; } /** * 通過路徑獲得文件夾, * 若不存在則拋異常, * 若存在則返回該文件夾。 * @param path * @return * @throws FileNotFoundException */ public static File getFolder(String path) throws FileNotFoundException{ path = separatorReplace(path); File folder = new File(path); if(!folder.isDirectory()){ throw new FileNotFoundException("folder not found!"); } return folder; } /** * 獲得文件最後更改時間。 * @param path * @return * @throws FileNotFoundException */ public static Date getFileLastModified(String path) throws FileNotFoundException{ path = separatorReplace(path); File file = getFile(path); return new Date(file.lastModified()); } /** * 獲得文件夾最後更改時間。 * @param path * @return * @throws FileNotFoundException */ public static Date getFolderLastModified(String path) throws FileNotFoundException{ path = separatorReplace(path); File folder = getFolder(path); return new Date(folder.lastModified()); } }

操作文件和文件夾