java建立檔案和目錄

來源:互聯網
上載者:User
建立檔案和目錄的關鍵技術點如下:
<pre name="code" class="java">    1、File類的createNewFile根據抽象路徑建立一個新的空檔案,當抽象路徑制定的檔案存在時,建立失敗    2、File類的mkdir方法根據抽象路徑建立目錄    3、File類的mkdirs方法根據抽象路徑建立目錄,包括建立必需但不存在的父目錄    4、File類的createTempFile方法建立臨時檔案,可以制定臨時檔案的檔案名稱首碼、尾碼及檔案所在的目錄,如果不指定目錄,則存放在系統的臨時檔案夾下。    5、除mkdirs方法外,以上方法在建立檔案和目錄時,必須保證目標檔案不存在,而且父目錄存在,否則會建立失敗   執行個體示範

 
package book.io;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 + "失敗。" + e.getMessage());            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;        if (dirName == null) {            try{                //在預設資料夾下建立臨時檔案                tempFile = File.createTempFile(prefix, suffix);                //返回臨時檔案的路徑                return tempFile.getCanonicalPath();            } catch (IOException e) {                e.printStackTrace();                System.out.println("建立臨時檔案失敗。" + e.getMessage());                return null;            }        } else {            File dir = new File(dirName);            //如果臨時檔案所在目錄不存在,首先建立            if (!dir.exists()) {                if (!CreateFileUtil.createDir(dirName)) {                    System.out.println("建立臨時檔案失敗,不能建立臨時檔案所在的目錄。");                    return null;                }            }            try {                //在指定目錄下建立臨時檔案                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 = "D:/work/temp/temp0/temp1";        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++) {            System.out.println("建立了臨時檔案:"                    + CreateFileUtil.createTempFile(prefix, suffix, dirName));        }        //在預設目錄下建立臨時檔案        for (int i = 0; i < 10; i++) {            System.out.println("在預設目錄下建立了臨時檔案:"                    + CreateFileUtil.createTempFile(prefix, suffix, null));        }    }}輸出結果:建立目錄D:/work/temp/temp0/temp1成功。目標檔案所在目錄不存在,準備建立它。建立單個檔案D:/work/temp/temp0/temp1/temp2/tempFile.txt成功。建立了臨時檔案:D:work emp emp0 emp1 emp5171.txt建立了臨時檔案:D:work emp emp0 emp1 emp5172.txt建立了臨時檔案:D:work emp emp0 emp1 emp5173.txt建立了臨時檔案:D:work emp emp0 emp1 emp5174.txt建立了臨時檔案:D:work emp emp0 emp1 emp5175.txt建立了臨時檔案:D:work emp emp0 emp1 emp5176.txt建立了臨時檔案:D:work emp emp0 emp1 emp5177.txt建立了臨時檔案:D:work emp emp0 emp1 emp5178.txt建立了臨時檔案:D:work emp emp0 emp1 emp5179.txt建立了臨時檔案:D:work emp emp0 emp1 emp5180.txt在預設目錄下建立了臨時檔案:C:Documents and SettingsAdministratorLocal SettingsTemp emp5181.txt在預設目錄下建立了臨時檔案:C:Documents and SettingsAdministratorLocal SettingsTemp emp5182.txt在預設目錄下建立了臨時檔案:C:Documents and SettingsAdministratorLocal SettingsTemp emp5183.txt在預設目錄下建立了臨時檔案:C:Documents and SettingsAdministratorLocal SettingsTemp emp5184.txt在預設目錄下建立了臨時檔案:C:Documents and SettingsAdministratorLocal SettingsTemp emp5185.txt在預設目錄下建立了臨時檔案:C:Documents and SettingsAdministratorLocal SettingsTemp emp5186.txt在預設目錄下建立了臨時檔案:C:Documents and SettingsAdministratorLocal SettingsTemp emp5187.txt在預設目錄下建立了臨時檔案:C:Documents and SettingsAdministratorLocal SettingsTemp emp5188.txt在預設目錄下建立了臨時檔案:C:Documents and SettingsAdministratorLocal SettingsTemp emp5189.txt在預設目錄下建立了臨時檔案:C:Documents and SettingsAdministratorLocal SettingsTemp emp5190.txt

聯繫我們

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