java文件操作類(新建 復制 移動 刪除文件和文件夾 獲取擴展名)

uyu 9年前發布 | 5K 次閱讀 Java

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
  • 文件操作類
  • (新建文件夾、刪除文件夾、復制文件夾、移動文件夾、新建文件、刪除文件、復制文件、移動文件、獲取文件擴展名、獲取文件路徑)
  • */ public class OperateFiles {

    /**

    • @param args */ public static void main(String[] args) { OperateFiles operateFiles = new OperateFiles(); // 新建一個文件夾 operateFiles.newFolder("c:/hongten"); // 新建一個文件,同時向里面寫入內容 operateFiles.newFile("c:/hongten/Hello.txt", "hello,i'm Hongten.你好"); // 刪除一個文件 operateFiles.deleteFile("c:/hongten/Hello.txt"); // 刪除一個文件夾 operateFiles.deleteFolder("c:/hongten"); // 復制文件夾 operateFiles.copyFolder("c:/hongten", "e:/hongten"); // 提取文件的擴展名 String expandedName = operateFiles

           .getExpandedName("c:/hongten/Hello.txt");
      

      System.out.println(expandedName); // 提取文件的路徑 System.out.println(operateFiles.getFilePath("c:/hongten/Hello.txt")); }

      /**

    • 獲得文件的擴展名
    • @param filePath
    • 文件的路徑 如:c:/hongten/Hello.txt
    • @return 文件的擴展名 如:txt */ public String getExpandedName(String filePath) { return filePath.substring(filePath.lastIndexOf(".") + 1); }

      /**

    • 獲得文件的路徑
    • @param file
    • 文件的路徑
    • @return 文件的路徑 */ public String getFilePath(String file) { return file.substring(0, file.lastIndexOf("/")); }

      /**

    • 新建一個目錄
    • @param folderPath
    • 新建目錄的路徑 如:c:\newFolder */ public void newFolder(String folderPath) { try { File myFolderPath = new File(folderPath.toString()); if (!myFolderPath.exists()) { myFolderPath.mkdir(); } } catch (Exception e) { System.out.println("新建目錄操作出錯"); e.printStackTrace(); } }

      /**

    • 新建一個文件
    • @param filePath
    • 新建文件的目錄 如:c:\hongten.java */ public void newFile(String filePath) { try { File myFilePathFile = new File(filePath.toString()); if (!myFilePathFile.exists()) { myFilePathFile.createNewFile(); } } catch (Exception e) { System.out.println("新文件創建失敗"); e.printStackTrace(); } }

      /**

    • 新建一個文件,同時向文件中寫入內容
    • @param filePath
    • 新建文件的目錄 如:c:\hongten.java
    • @param fileContent
    • 向文件中寫入的內容 */ public void newFile(String filePath, String fileContent) { try { newFile(filePath); FileWriter resultFile = new FileWriter(filePath); PrintWriter myFile = new PrintWriter(resultFile); myFile.println(fileContent); resultFile.close(); } catch (Exception e) { System.out.println("新建文件操作出錯"); e.printStackTrace(); } }

      /**

    • 刪除一個文件
    • @param filePath
    • 要刪除文件的絕對路徑 如:c:\hongten\Hello.txt */ public void deleteFile(String filePath) { try { File preDelFile = new File(filePath); if (preDelFile.exists()) { preDelFile.delete(); } else { System.out.println(filePath + "不存在!"); } } catch (Exception e) { System.out.println("刪除文件操作出錯"); e.printStackTrace(); } }

      /**

    • 刪除一個文件夾
    • @param folderPath
    • 要刪除的文件夾的絕對路徑 如:c:\hongten */ public void deleteFolder(String folderPath) { try { delAllFiles(folderPath); File preDelFoder = new File(folderPath); if (preDelFoder.exists()) { preDelFoder.delete(); } else { System.out.println(folderPath + "不存在!"); } } catch (Exception e) { System.out.println("刪除文件操作出錯"); e.printStackTrace(); } }

      /**

    • 刪除一個文件夾下的所有文件
    • @param folderPath
    • 要刪除的文件夾的絕對路徑 如:c:\hongten */ public void delAllFiles(String folderPath) { File file = new File(folderPath); if (!file.exists()) { return; } if (!file.isDirectory()) { return; } String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (folderPath.endsWith(File.separator)) { temp = new File(folderPath + tempList[i]); } else { temp = new File(folderPath + File.separator + tempList[i]); } if (temp.isFile()) { temp.delete(); } if (temp.isDirectory()) { delAllFiles(folderPath + "/" + tempList[i]);// 先刪除文件夾里面的文件 deleteFolder(folderPath + "/" + tempList[i]);// 再刪除空文件夾 } } }

      /**

    • 單個文件的復制
    • @param oldPath
    • 原路徑 如:c:\Hello.java
    • @param newPath
    • 新路徑 如:f:\Hello.java */ public void copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { // 文件存在時 InputStream inStream = new FileInputStream(oldPath); // 讀入原文件 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; // int length = 0; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; // 字節數 文件大小 fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { System.out.println("復制單個文件操作出錯"); e.printStackTrace();

      } }

      /**

    • 文件夾的復制
    • @param oldPath
    • 原文件夾路徑 如: c:\hello
    • @param newPath
    • 新文件夾路徑 如: e:\hello */ public void copyFolder(String oldPath, String newPath) {

      try { (new File(newPath)).mkdirs(); // 如果文件夾不存在 則建立新文件夾 File a = new File(oldPath); String[] file = a.list(); File temp = null; for (int i = 0; i < file.length; i++) { if (oldPath.endsWith(File.separator)) { temp = new File(oldPath + file[i]); } else { temp = new File(oldPath + File.separator + file[i]); }

      if (temp.isFile()) { FileInputStream input = new FileInputStream(temp); FileOutputStream output = new FileOutputStream(newPath

            + "/" + (temp.getName()).toString());
      

      byte[] b = new byte[1024 * 5]; int len; while ((len = input.read(b)) != -1) {

        output.write(b, 0, len);
      

      } output.flush(); output.close(); input.close(); } if (temp.isDirectory()) {// 如果是子文件夾 copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]); } } } catch (Exception e) { System.out.println("復制整個文件夾內容操作出錯"); e.printStackTrace();

      } }

      /**

    • 移動單個文件
    • @param oldPath
    • 源文件路徑 如:c:\hello.java
    • @param newPath
    • 新文件路徑 如:e:\hello.java */ public void moveFile(String oldPath, String newPath) { copyFile(oldPath, newPath); deleteFile(oldPath); }

      /**

    • 移動文件夾
    • @param oldPath
    • 原文件夾路徑 如:c:\hongten
    • @param newPath
    • 新文件夾路徑 如:e:\hongten */ public void moveFolder(String oldPath, String newPath) { copyFolder(oldPath, newPath); deleteFolder(oldPath); }

      /**

    • 獲得系統根目錄絕對路徑
    • @return */ public String getPath() { String sysPath = this.getClass().getResource("/").getPath(); // 對路徑進行修改 sysPath = sysPath.substring(1, sysPath.length() - 16); return sysPath; }

}</pre>

 本文由用戶 uyu 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!