使用java代碼在本地建立檔案夾(多層目錄),在本地磁碟建立檔案目錄

來源:互聯網
上載者:User

使用java代碼在本地組建檔案夾

import java.io.File;import java.io.IOException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/**  * @ClassName   CreateFileUtil.java * @Author      Lina * @Describtion 在本地磁碟建立檔案目錄 * @Date        建立時間:2017-7-12 下午4:06:40  * */public class CreateFileUtil {private static final Logger logger = LoggerFactory.getLogger(CreateFileUtil.class);public static boolean createFile(String destFileName) {          File file = new File(destFileName);          if(file.exists()) {              logger.warn("建立單個檔案" + destFileName + "失敗,目標檔案已存在。");              return false;          }          if (destFileName.endsWith(File.separator)) {              logger.warn("建立單個檔案" + destFileName + "失敗,目標檔案不能為目錄。");              return false;          }          //判斷目標檔案所在的目錄是否存在          if(!file.getParentFile().exists()) {              //如果目標檔案所在的目錄不存在,則建立父目錄              logger.warn("目標檔案所在目錄不存在,準備建立它。");              if(!file.getParentFile().mkdirs()) {                  logger.warn("建立目標檔案所在目錄失敗。");                  return false;              }          }          //建立目標檔案          try {              if (file.createNewFile()) {                  logger.warn("建立單個檔案" + destFileName + "成功。");                  return true;              } else {                  logger.warn("建立單個檔案" + destFileName + "失敗。");                  return false;              }          } catch (IOException e) {              e.printStackTrace();              logger.warn("建立單個檔案" + destFileName + "失敗。" + e.getMessage());              return false;          }      }                public static boolean createDir(String destDirName) {          File dir = new File(destDirName);          if (dir.exists()) {              logger.warn("建立目錄" + destDirName + "失敗,目標目錄已經存在");              return false;          }          if (!destDirName.endsWith(File.separator)) {              destDirName = destDirName + File.separator;          }          //建立目錄          if (dir.mkdirs()) {              logger.warn("建立目錄" + destDirName + "成功。");              return true;          } else {              logger.warn("建立目錄" + destDirName + "失敗。");              return false;          }      }                public static String createTempFile(String prefix, String suffix, String dirName) {          File tempFile = null;          if (dirName == null) {              try{                  //在預設資料夾下建立臨時檔案                  tempFile = File.createTempFile(prefix, suffix);                  //返回臨時檔案的路徑                  return tempFile.getCanonicalPath();              } catch (IOException e) {                  e.printStackTrace();                  logger.warn("建立臨時檔案失敗。" + e.getMessage());                  return null;              }          } else {              File dir = new File(dirName);              //如果臨時檔案所在目錄不存在,首先建立              if (!dir.exists()) {                  if (!CreateFileUtil.createDir(dirName)) {                      logger.warn("建立臨時檔案失敗,不能建立臨時檔案所在的目錄。");                      return null;                  }              }              try {                  //在指定目錄下建立臨時檔案                  tempFile = File.createTempFile(prefix, suffix, dir);                  return tempFile.getCanonicalPath();              } catch (IOException e) {                  e.printStackTrace();                  logger.warn("建立臨時檔案失敗。" + e.getMessage());                  return null;              }          }      }           public static void main(String[] args) {          //建立目錄          String dirName = "C:/Users/admin/Desktop/建立的檔案夾名稱";          CreateFileUtil.createDir(dirName);          //建立檔案          String fileName = dirName + "/temp2/tempFile.txt";          CreateFileUtil.createFile(fileName);  //        //建立臨時檔案  //        String prefix = "temp";  //        String suffix = ".txt";  //        for (int i = 0; i < 10; i++) {  //            logger.warn("建立了臨時檔案:"  //                    + CreateFileUtil.createTempFile(prefix, suffix, dirName));  //        }  //        //在預設目錄下建立臨時檔案  //        for (int i = 0; i < 10; i++) {  //            System.out.println("在預設目錄下建立了臨時檔案:"  //                    + CreateFileUtil.createTempFile(prefix, suffix, null));  //        }      } }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.