標籤:sts wpa for data 我愛你 exist 輸入資料流 工具 ret
在使用java進行開發時常常會用到檔案和目錄的增刪複製等方法。我寫了一個小工具類。和大家分享,希望大家指正:
package com.wangpeng.utill;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.InputStream;import java.io.PrintWriter;/** * @author wangpeng * */public class ToolOfFile {/** * 建立目錄 * * @param folderPath * 目錄目錄 * @throws Exception */public static void newFolder(String folderPath) throws Exception {try {java.io.File myFolder = new java.io.File(folderPath);if (!myFolder.exists()) {myFolder.mkdir();}} catch (Exception e) {throw e;}}/** * 建立檔案 * * @param filePath * 檔案路徑 * @throws Exception */public static void newFile(String filePath) throws Exception {try {File myFile = new File(filePath);if (!myFile.exists()) {myFile.createNewFile();}} catch (Exception e) {throw e;}}/** * 建立檔案,並寫入內容 * * @param filePath * 檔案路徑 * @param fileContent * 被寫入的檔案內容 * @throws Exception */public static void newFile(String filePath, String fileContent)throws Exception {// 用來寫入字元檔案的便捷類FileWriter fileWriter = null;// 向文本輸出資料流列印對象的格式化表示形式,使用指定檔案建立不具有自己主動行重新整理的新PrintWriter printWriter = null;try {File myFile = new File(filePath);if (!myFile.exists()) {myFile.createNewFile();}fileWriter = new FileWriter(myFile);printWriter = new PrintWriter(fileWriter);printWriter.print(fileContent);printWriter.flush();} catch (Exception e) {throw e;} finally {if (printWriter != null) {printWriter.close();}if (fileWriter != null) {fileWriter.close();}}}/** * 複製檔案 * * @param oldPath * 被拷貝的檔案 * @param newPath * 複製到的檔案 * @throws Exception */public static void copyFile(String oldPath, String newPath)throws Exception {InputStream inStream = null;FileOutputStream outStream = null;try {int byteread = 0;File oldfile = new File(oldPath);// 檔案存在時if (oldfile.exists()) {inStream = new FileInputStream(oldfile);outStream = new FileOutputStream(newPath);byte[] buffer = new byte[1444];while ((byteread = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, byteread);}outStream.flush();}} catch (Exception e) {throw e;} finally {if (outStream != null) {outStream.close();}if (inStream != null) {inStream.close();}}}/** * 複製檔案 * @param inStream 被拷貝的檔案的輸入資料流 * @param newPath 被複製到的目標 * @throws Exception */public static void copyFile(InputStream inStream, String newPath)throws Exception {FileOutputStream outStream = null;try {int byteread = 0;outStream = new FileOutputStream(newPath);byte[] buffer = new byte[1444];while ((byteread = inStream.read(buffer)) != -1) {outStream.write(buffer, 0, byteread);}outStream.flush();} catch (Exception e) {throw e;} finally {if (outStream != null) {outStream.close();}if (inStream != null) {inStream.close();}}}/** * 複製目錄 * * @param oldPath * 被複製的目錄路徑 * @param newPath * 被複製到的目錄路徑 * @throws Exception */public static void copyFolder(String oldPath, String newPath)throws Exception {try {(new File(newPath)).mkdirs(); // 假設目錄不存在 則建立新目錄File a = new File(oldPath);String[] file = a.list();File tempIn = null;for (int i = 0; i < file.length; i++) {if (oldPath.endsWith(File.separator)) {tempIn = new File(oldPath + file[i]);} else {tempIn = new File(oldPath + File.separator + file[i]);}if (tempIn.isFile()) {copyFile(tempIn.getAbsolutePath(),newPath + "/" + (tempIn.getName()).toString());} else if (tempIn.isDirectory()) {// 假設是子目錄copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);}}} catch (Exception e) {throw e;}}/** * 刪除檔案 * * @param filePathAndName */public static void delFileX(String filePathAndName) {File myDelFile = new File(filePathAndName);myDelFile.delete();}/** * 刪除目錄 * * @param path */public static void delForder(String path) {File delDir = new File(path);if (!delDir.exists()) {return;}if (!delDir.isDirectory()) {return;}String[] tempList = delDir.list();File temp = null;for (int i = 0; i < tempList.length; i++) {if (path.endsWith(File.separator)) {temp = new File(path + tempList[i]);} else {temp = new File(path + File.separator + tempList[i]);}if (temp.isFile()) {temp.delete();} else if (temp.isDirectory()) {// 刪除完裡面全部內容delForder(path + "/" + tempList[i]);}}delDir.delete();}public static void main(String[] args) {String oldPath = "F:/test/aaa/";String newPath = "F:/test/bbb/";try {// ToolOfFile.newFolder("F:/test/hello/");// ToolOfFile.newFile("F:/test/hello/world.txt","我愛你,the world!");ToolOfFile.copyFolder(oldPath, newPath);// ToolOfFile.delForder("F:/test/hello");} catch (Exception e) {e.printStackTrace();}System.out.println("OK");}}
java檔案和目錄的增刪複製