Java建立檔案夾及檔案

來源:互聯網
上載者:User
package com.xhkj.util;import java.io.File;import java.io.IOException;public class CreateFileUtil {public static boolean CreateFile(String destFileName) {    File file = new File(destFileName);    if (file.exists()) {     System.out.println("建立單個檔案" + destFileName + "失敗,目標檔案已存在!");     return false;    }    if (destFileName.endsWith(File.separator)) {     System.out.println("建立單個檔案" + destFileName + "失敗,目標不能是目錄!");     return false;    }    if (!file.getParentFile().exists()) {     System.out.println("目標檔案所在路徑不存在,準備建立。。。");     if (!file.getParentFile().mkdirs()) {      System.out.println("建立目錄檔案所在的目錄失敗!");      return false;     }    }    // 建立目標檔案    try {     if (file.createNewFile()) {      System.out.println("建立單個檔案" + destFileName + "成功!");      return true;     } else {      System.out.println("建立單個檔案" + destFileName + "失敗!");      return false;     }    } catch (IOException e) {     e.printStackTrace();     System.out.println("建立單個檔案" + destFileName + "失敗!");     return false;    }}public static boolean createDir(String destDirName) {    File dir = new File(destDirName);    if(dir.exists()) {     System.out.println("建立目錄" + destDirName + "失敗,目標目錄已存在!");     return false;    }    if(!destDirName.endsWith(File.separator))     destDirName = destDirName + File.separator;    // 建立單個目錄    if(dir.mkdirs()) {     System.out.println("建立目錄" + destDirName + "成功!");     return true;    } else {     System.out.println("建立目錄" + destDirName + "成功!");     return false;    }}public static String createTempFile(String prefix, String suffix, String dirName) {    File tempFile = null;    try{    if(dirName == null) {     // 在預設資料夾下建立臨時檔案     tempFile = File.createTempFile(prefix, suffix);     return tempFile.getCanonicalPath();    }    else {     File dir = new File(dirName);     // 如果臨時檔案所在目錄不存在,首先建立     if(!dir.exists()) {      if(!CreateFileUtil.createDir(dirName)){       System.out.println("建立臨時檔案失敗,不能建立臨時檔案所在目錄!");       return null;      }     }     tempFile = File.createTempFile(prefix, suffix, dir);     return tempFile.getCanonicalPath();    }    } catch(IOException e) {     e.printStackTrace();     System.out.println("建立臨時檔案失敗" + e.getMessage());     return null;    }}public static void main(String[] args) {    // 建立目錄    String dirName = "c:/test/test0/test1";    CreateFileUtil.createDir(dirName);    // 建立檔案    String fileName = dirName + "/test2/testFile.txt";    CreateFileUtil.CreateFile(fileName);    // 建立臨時檔案    String prefix = "temp";    String suffix = ".txt";    for(int i = 0; i < 10; i++) {     System.out.println("建立了臨時檔案:" + CreateFileUtil.createTempFile(prefix, suffix, dirName));    }  }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.