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

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

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

復(fù)制代碼 代碼如下:

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("新建目錄操作出錯(cuò)");
      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("新建目錄操作出錯(cuò)");
      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("刪除文件操作出錯(cuò)");
      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("刪除文件夾操作出錯(cuò)");
      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]);//再刪除空文件夾
      }
    }
  }

  /**
   * 復(fù)制單個(gè)文件
   * @param oldPath String 原文件路徑 如:c:/fqf.txt
   * @param newPath String 復(fù)制后路徑 如: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()) { //文件存在時(shí)
        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("復(fù)制單個(gè)文件操作出錯(cuò)");
      e.printStackTrace();

    }

  }

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

    }

  }

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

  }

  /**
   * 移動(dòng)文件到指定目錄
   * @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è)計(jì)模式七大原則之單一職責(zé)原則詳解

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    給Java文件打成獨(dú)立JAR包的詳細(xì)步驟記錄

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

最新評(píng)論

永州市| 婺源县| 三都| 赤峰市| 如皋市| 绩溪县| 沙洋县| 兴化市| 高雄市| 措勤县| 达孜县| 五常市| 延吉市| 克什克腾旗| 珲春市| 娄底市| 连江县| 横峰县| 鄱阳县| 集贤县| 光山县| 永平县| 曲松县| 格尔木市| 河间市| 股票| 西乌珠穆沁旗| 垣曲县| 宁明县| 马鞍山市| 乌恰县| 邵东县| 林州市| 柏乡县| 车致| 略阳县| 佛学| 丹阳市| 布尔津县| 天气| 肇东市|