国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

基于java file 文件操作operate file of java的應用

2019-11-26 16:08:45
字體:
來源:轉載
供稿:網友
java文件操作
復制代碼 代碼如下:

package com.b510;

 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 Hongten</br>
  *
  *         文件的操作
  *
  *         
  *
  */
 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;
     }

 }

現在有時間把這些東西整理出來,給大家分享一下……
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 三河市| 荣昌县| 姚安县| 高淳县| 柳州市| 嘉义县| 晋宁县| 盘锦市| 平武县| 上饶县| 牟定县| 峨边| 嘉荫县| 冷水江市| 建阳市| 浮梁县| 花莲县| 淄博市| 玛多县| 涿州市| 洛扎县| 沿河| 绥江县| 陇川县| 长春市| 温泉县| 广宗县| 聂荣县| 黑河市| 松溪县| 辉南县| 光山县| 忻城县| 福清市| 广东省| 泾源县| 土默特右旗| 佛冈县| 徐水县| 满城县| 南安市|