天天看點

檔案以及檔案夾的建立。目錄下所有檔案和子目錄查詢

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()) {  //mkdirs()可以建立多級檔案夾, mkdir()隻會建立一級的檔案夾

            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 (!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 = "G:/test";  

        createDir(dirName);  

        //建立檔案  

        String fileName = dirName + "/test1/test1.txt";  

        createFile(fileName);  

        //建立臨時檔案  

        String prefix = "temp";  

        String suffix = ".txt";  

        for (int i = 0; i < 10; i++) {  

            System.out.println("建立了臨時檔案:"  

                    + createTempFile(prefix, suffix, dirName));  

        }  

        //在預設目錄下建立臨時檔案  

        for (int i = 0; i < 10; i++) {  

            System.out.println("在預設目錄下建立了臨時檔案:"  

                    + createTempFile(prefix, suffix, null));  

        }  

    }

//顯示目錄下所有子目錄和檔案

    public static void tree(File f){

        //判斷傳入對象是否為一個檔案夾對象

        if(!f.isDirectory()){//判斷是否是目錄

            System.out.println("你輸入的不是一個檔案夾,請檢查路徑是否有誤!!");

        }

        else{

            File[] t = f.listFiles();

            for(int i=0;i<t.length;i++){

                //判斷檔案清單中的對象是否為檔案夾對象,如果是則執行tree遞歸,直到把此檔案夾中所有檔案輸出為止

                if(t[i].isDirectory()){//目錄

                    System.out.println("package\t"+t[i].getName());

                    tree(t[i]);

                }

                else{

                    System.out.println("File\t"+t[i].getName());

                }

            }

        }

    }