最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java怎么創(chuàng)建目錄(刪除/修改/復制目錄及文件)代碼實例

 更新時間:2013年12月10日 10:17:12   作者:  
這篇文章主要介紹了java怎么創(chuàng)建目錄,還包括刪除/修改/復制目錄及文件,代碼簡單,下面直接看代碼吧

復制代碼 代碼如下:

import java.io.*;

public class FileOperate {
  public FileOperate() {
  }

  /**
   * 新建目錄
   * @param folderPath String 如 c:/fqf
   * @return boolean
   */
  public void newFolder(String folderPath) {
    try {
      String filePath = folderPath;
      filePath = filePath.toString();
      java.io.File myFilePath = new java.io.File(filePath);
      if (!myFilePath.exists()) {
        myFilePath.mkdir();
      }
    }
    catch (Exception e) {
      System.out.println("新建目錄操作出錯");
      e.printStackTrace();
    }
  }

  /**
   * 新建文件
   * @param filePathAndName String 文件路徑及名稱 如c:/fqf.txt
   * @param fileContent String 文件內(nèi)容
   * @return boolean
   */
  public void newFile(String filePathAndName, String fileContent) {

    try {
      String filePath = filePathAndName;
      filePath = filePath.toString();
      File myFilePath = new File(filePath);
      if (!myFilePath.exists()) {
        myFilePath.createNewFile();
      }
      FileWriter resultFile = new FileWriter(myFilePath);
      PrintWriter myFile = new PrintWriter(resultFile);
      String strContent = fileContent;
      myFile.println(strContent);
      resultFile.close();

    }
    catch (Exception e) {
      System.out.println("新建目錄操作出錯");
      e.printStackTrace();

    }

  }

  /**
   * 刪除文件
   * @param filePathAndName String 文件路徑及名稱 如c:/fqf.txt
   * @param fileContent String
   * @return boolean
   */
  public void delFile(String filePathAndName) {
    try {
      String filePath = filePathAndName;
      filePath = filePath.toString();
      java.io.File myDelFile = new java.io.File(filePath);
      myDelFile.delete();

    }
    catch (Exception e) {
      System.out.println("刪除文件操作出錯");
      e.printStackTrace();

    }

  }

  /**
   * 刪除文件夾
   * @param filePathAndName String 文件夾路徑及名稱 如c:/fqf
   * @param fileContent String
   * @return boolean
   */
  public void delFolder(String folderPath) {
    try {
      delAllFile(folderPath); //刪除完里面所有內(nèi)容
      String filePath = folderPath;
      filePath = filePath.toString();
      java.io.File myFilePath = new java.io.File(filePath);
      myFilePath.delete(); //刪除空文件夾

    }
    catch (Exception e) {
      System.out.println("刪除文件夾操作出錯");
      e.printStackTrace();

    }

  }

  /**
   * 刪除文件夾里面的所有文件
   * @param path String 文件夾路徑 如 c:/fqf
   */
  public void delAllFile(String path) {
    File file = new File(path);
    if (!file.exists()) {
      return;
    }
    if (!file.isDirectory()) {
      return;
    }
    String[] tempList = file.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();
      }
      if (temp.isDirectory()) {
        delAllFile(path+"/"+ tempList[i]);//先刪除文件夾里面的文件
        delFolder(path+"/"+ tempList[i]);//再刪除空文件夾
      }
    }
  }

  /**
   * 復制單個文件
   * @param oldPath String 原文件路徑 如:c:/fqf.txt
   * @param newPath String 復制后路徑 如:f:/fqf.txt
   * @return boolean
   */
  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;
        while ( (byteread = inStream.read(buffer)) != -1) {
          bytesum += byteread; //字節(jié)數(shù) 文件大小
          System.out.println(bytesum);
          fs.write(buffer, 0, byteread);
        }
        inStream.close();
      }
    }
    catch (Exception e) {
      System.out.println("復制單個文件操作出錯");
      e.printStackTrace();

    }

  }

  /**
   * 復制整個文件夾內(nèi)容
   * @param oldPath String 原文件路徑 如:c:/fqf
   * @param newPath String 復制后路徑 如:f:/fqf/ff
   * @return boolean
   */
  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("復制整個文件夾內(nèi)容操作出錯");
      e.printStackTrace();

    }

  }

  /**
   * 移動文件到指定目錄
   * @param oldPath String 如:c:/fqf.txt
   * @param newPath String 如:d:/fqf.txt
   */
  public void moveFile(String oldPath, String newPath) {
    copyFile(oldPath, newPath);
    delFile(oldPath);

  }

  /**
   * 移動文件到指定目錄
   * @param oldPath String 如:c:/fqf.txt
   * @param newPath String 如:d:/fqf.txt
   */
  public void moveFolder(String oldPath, String newPath) {
    copyFolder(oldPath, newPath);
    delFolder(oldPath);

  }
}

相關(guān)文章

  • Java設(shè)計模式七大原則之單一職責原則詳解

    Java設(shè)計模式七大原則之單一職責原則詳解

    單一職責原則(Single Responsibility Principle, SRP),有且僅有一個原因引起類的變更。簡單來說,就是針對一個java類,它應(yīng)該只負責一項職責。本文將詳細介紹一下Java設(shè)計模式七大原則之一的單一職責原則,需要的可以參考一下
    2022-02-02
  • Java為什么占用四個字節(jié)你知道嗎

    Java為什么占用四個字節(jié)你知道嗎

    這篇文章主要介紹了Java為什么占四個字節(jié),文中介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-08-08
  • Javaweb實現(xiàn)完整個人博客系統(tǒng)流程

    Javaweb實現(xiàn)完整個人博客系統(tǒng)流程

    這篇文章主要介紹了怎樣用Java來實現(xiàn)一個完整的個人博客系統(tǒng),我們通過實操上手的方式可以高效的鞏固所學的基礎(chǔ)知識,感興趣的朋友一起來看看吧
    2022-03-03
  • Java簡單實現(xiàn)銀行ATM系統(tǒng)

    Java簡單實現(xiàn)銀行ATM系統(tǒng)

    這篇文章主要為大家詳細介紹了Java簡單實現(xiàn)銀行ATM系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • SpringCloud微服務(wù)踩坑記錄分享

    SpringCloud微服務(wù)踩坑記錄分享

    本文記錄了作者在使用SpringCloud微服務(wù)時遇到的問題,首先,作者嘗試修改配置文件中的service-name和instance-id,但仍然無法解決問題,后來,作者嘗試更換SpringCloud版本為2.2.5,并搭配Hoxton.SR3版本,問題得以解決
    2024-11-11
  • SpringMVC異常處理的三種方式小結(jié)

    SpringMVC異常處理的三種方式小結(jié)

    本文主要介紹了SpringMVC異常處理的三種方式小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-09-09
  • Java實現(xiàn)對視頻進行截圖的方法【附ffmpeg下載】

    Java實現(xiàn)對視頻進行截圖的方法【附ffmpeg下載】

    這篇文章主要介紹了Java實現(xiàn)對視頻進行截圖的方法,結(jié)合實例形式分析了Java使用ffmpeg針對視頻進行截圖的相關(guān)操作技巧,并附帶ffmpeg.exe文件供讀者下載使用,需要的朋友可以參考下
    2018-01-01
  • Java中Prime算法的原理與實現(xiàn)詳解

    Java中Prime算法的原理與實現(xiàn)詳解

    Prime算法是一種窮舉查找算法來從一個連通圖中構(gòu)造一棵最小生成樹。本文主要為大家介紹了Java中Prime算法的原理與實現(xiàn),感興趣的可以學習一下
    2022-07-07
  • SpringBoot自動裝配原理小結(jié)

    SpringBoot自動裝配原理小結(jié)

    Spring Boot主要作用就是簡化Spring應(yīng)用的開發(fā),開發(fā)者只需要通過少量代碼就可以創(chuàng)建一個Spring應(yīng)用,而達到這一目的最核心的思想就是約定優(yōu)于配置。
    2021-05-05
  • 給Java文件打成獨立JAR包的詳細步驟記錄

    給Java文件打成獨立JAR包的詳細步驟記錄

    這篇文章主要介紹了給Java文件打成獨立JAR包的相關(guān)資料,文中將Java文件打包成獨立的JAR包,包括非Maven和Maven項目的打包步驟,需要的朋友可以參考下
    2024-12-12

最新評論

罗山县| 武威市| 辰溪县| 康乐县| 甘谷县| 仁布县| 合山市| 义乌市| 西畴县| 龙山县| 滦平县| 南漳县| 凤城市| 尖扎县| 衢州市| 达孜县| 平原县| 中卫市| 莎车县| 辉南县| 寻乌县| 丘北县| 聂拉木县| 焦作市| 华池县| 吴忠市| 广灵县| 太原市| 闻喜县| 新乐市| 玉门市| 志丹县| 澄江县| 稷山县| 岐山县| 瑞丽市| 荣昌县| 浙江省| 麟游县| 大城县| 尼玛县|