欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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語法基礎(chǔ)之循環(huán)結(jié)構(gòu)語句詳解

    Java語法基礎(chǔ)之循環(huán)結(jié)構(gòu)語句詳解

    這篇文章主要為大家詳細(xì)介紹了Java語法基礎(chǔ)之循環(huán)結(jié)構(gòu)語句,感興趣的小伙伴們可以參考一下
    2016-09-09
  • SpringBoot+Vue項(xiàng)目部署實(shí)現(xiàn)傳統(tǒng)方式

    SpringBoot+Vue項(xiàng)目部署實(shí)現(xiàn)傳統(tǒng)方式

    我們?cè)谶M(jìn)行前后端分離開發(fā)的時(shí)候,一般是將前端項(xiàng)目部署到nginx服務(wù)器上,與后端項(xiàng)目分開部署,這篇文章主要給大家介紹了關(guān)于SpringBoot+Vue項(xiàng)目部署實(shí)現(xiàn)傳統(tǒng)方式的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • IDEA調(diào)試功能使用總結(jié)(step?over/step?into/force?step?into/step?out)

    IDEA調(diào)試功能使用總結(jié)(step?over/step?into/force?step?into/step?o

    本文主要介紹了IDEA調(diào)試功能使用總結(jié)(step?over/step?into/force?step?into/step?out),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • aop注解方式實(shí)現(xiàn)全局日志管理方法

    aop注解方式實(shí)現(xiàn)全局日志管理方法

    下面小編就為大家分享一篇aop注解方式實(shí)現(xiàn)全局日志管理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • java DataInputStream和DataOutputStream詳解及實(shí)例代碼

    java DataInputStream和DataOutputStream詳解及實(shí)例代碼

    這篇文章主要介紹了java DataInputStream和DataOutputStream詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 微信支付java版本之查詢訂單

    微信支付java版本之查詢訂單

    這篇文章主要為大家詳細(xì)介紹了微信支付java版本之查詢訂單,為大家分享了微信支付訂單的查詢接口,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Java時(shí)間復(fù)雜度、空間復(fù)雜度的深入詳解

    Java時(shí)間復(fù)雜度、空間復(fù)雜度的深入詳解

    對(duì)于一個(gè)算法,其時(shí)間復(fù)雜度和空間復(fù)雜度往往是相互影響的,當(dāng)追求一個(gè)較好的時(shí)間復(fù)雜度時(shí),可能會(huì)使空間復(fù)雜度的性能變差,即可能導(dǎo)致占用較多的存儲(chǔ)空間,這篇文章主要給大家介紹了關(guān)于Java時(shí)間復(fù)雜度、空間復(fù)雜度的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • SpringBoot使用jsoup爬取HTML的方法

    SpringBoot使用jsoup爬取HTML的方法

    jsoup 是一款 Java 的 HTML 解析器,它提供了一套非常便利的 API,可通過 DOM、CSS 通過類似于 JQuery 的操作方法來取出和操作數(shù)據(jù),這篇文章主要介紹了SpringBoot使用jsoup爬取HTML,需要的朋友可以參考下
    2024-02-02
  • Spark學(xué)習(xí)筆記 (二)Spark2.3 HA集群的分布式安裝圖文詳解

    Spark學(xué)習(xí)筆記 (二)Spark2.3 HA集群的分布式安裝圖文詳解

    這篇文章主要介紹了Spark2.3 HA集群的分布式安裝,結(jié)合圖文與實(shí)例形式詳細(xì)分析了Spark2.3 HA集群分布式安裝具體下載、安裝、配置、啟動(dòng)及執(zhí)行spark程序等相關(guān)操作技巧,需要的朋友可以參考下
    2020-02-02
  • Java中判斷對(duì)象是否為空的方法的詳解

    Java中判斷對(duì)象是否為空的方法的詳解

    這篇文章主要介紹了Java中判斷對(duì)象是否為空的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04

最新評(píng)論