日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

java創(chuàng)建文件和目錄

 西門獨(dú)孤 2011-01-03

java創(chuàng)建文件和目錄

創(chuàng)建文件和目錄的關(guān)鍵技術(shù)點(diǎn)如下:
    1、File類的createNewFile根據(jù)抽象路徑創(chuàng)建一個(gè)新的空文件,當(dāng)抽象路徑制定的文件存在時(shí),創(chuàng)建失敗
    2、File類的mkdir方法根據(jù)抽象路徑創(chuàng)建目錄
    3、File類的mkdirs方法根據(jù)抽象路徑創(chuàng)建目錄,包括創(chuàng)建必需但不存在的父目錄
    4、File類的createTempFile方法創(chuàng)建臨時(shí)文件,可以制定臨時(shí)文件的文件名前綴、后綴及文件所在的目錄,如果不指定目錄,則存放在系統(tǒng)的臨時(shí)文件夾下。
    5、除mkdirs方法外,以上方法在創(chuàng)建文件和目錄時(shí),必須保證目標(biāo)文件不存在,而且父目錄存在,否則會(huì)創(chuàng)建失敗
   
實(shí)例演示

 

 

package book.io;

import java.io.File;
import java.io.IOException;

/** *//**
 * 創(chuàng)建新文件和目錄
 * @author joe
 *
 */

public class CreateFileUtil ...{
    /** *//**
     * 創(chuàng)建單個(gè)文件
     * @param destFileName    目標(biāo)文件名
     * @return    創(chuàng)建成功,返回true,否則返回false
     */
    public static boolean createFile(String destFileName) ...{
        File file = new File(destFileName);
        if(file.exists()) ...{
            System.out.println("創(chuàng)建單個(gè)文件" + destFileName + "失敗,目標(biāo)文件已存在!");
            return false;
        }
        if (destFileName.endsWith(File.separator)) ...{
            System.out.println("創(chuàng)建單個(gè)文件" + destFileName + "失敗,目標(biāo)文件不能為目錄!");
            return false;
        }
        //判斷目標(biāo)文件所在的目錄是否存在
        if(!file.getParentFile().exists()) ...{
            //如果目標(biāo)文件所在的目錄不存在,則創(chuàng)建父目錄
            System.out.println("目標(biāo)文件所在目錄不存在,準(zhǔn)備創(chuàng)建它!");
            if(!file.getParentFile().mkdirs()) ...{
                System.out.println("創(chuàng)建目標(biāo)文件所在目錄失??!");
                return false;
            }
        }
        //創(chuàng)建目標(biāo)文件
        try ...{
            if (file.createNewFile()) ...{
                System.out.println("創(chuàng)建單個(gè)文件" + destFileName + "成功!");
                return true;
            } else ...{
                System.out.println("創(chuàng)建單個(gè)文件" + destFileName + "失??!");
                return false;
            }
        } catch (IOException e) ...{
            e.printStackTrace();
            System.out.println("創(chuàng)建單個(gè)文件" + destFileName + "失??!" + e.getMessage());
            return false;
        }
    }
   
    /** *//**
     * 創(chuàng)建目錄
     * @param destDirName    目標(biāo)目錄名
     * @return    目錄創(chuàng)建成功返回true,否則返回false
     */
    public static boolean createDir(String destDirName) ...{
        File dir = new File(destDirName);
        if (dir.exists()) ...{
            System.out.println("創(chuàng)建目錄" + destDirName + "失敗,目標(biāo)目錄已經(jīng)存在");
            return false;
        }
        if (!destDirName.endsWith(File.separator)) ...{
            destDirName = destDirName + File.separator;
        }
        //創(chuàng)建目錄
        if (dir.mkdirs()) ...{
            System.out.println("創(chuàng)建目錄" + destDirName + "成功!");
            return true;
        } else ...{
            System.out.println("創(chuàng)建目錄" + destDirName + "失??!");
            return false;
        }
    }
   
    /** *//**
     * 創(chuàng)建臨時(shí)文件
     * @param prefix    臨時(shí)文件名的前綴
     * @param suffix    臨時(shí)文件名的后綴
     * @param dirName    臨時(shí)文件所在的目錄,如果輸入null,則在用戶的文檔目錄下創(chuàng)建臨時(shí)文件
     * @return    臨時(shí)文件創(chuàng)建成功返回臨時(shí)文件路徑及文件名,否則返回null
     */
    public static String createTempFile(String prefix, String suffix, String dirName) ...{
        File tempFile = null;
        if (dirName == null) ...{
            try...{
                //在默認(rèn)文件夾下創(chuàng)建臨時(shí)文件
                tempFile = File.createTempFile(prefix, suffix);
                //返回臨時(shí)文件的路徑
                return tempFile.getCanonicalPath();
            } catch (IOException e) ...{
                e.printStackTrace();
                System.out.println("創(chuàng)建臨時(shí)文件失?。? + e.getMessage());
                return null;
            }
        } else ...{
            File dir = new File(dirName);
            //如果臨時(shí)文件所在目錄不存在,首先創(chuàng)建
            if (!dir.exists()) ...{
                if (!CreateFileUtil.createDir(dirName)) ...{
                    System.out.println("創(chuàng)建臨時(shí)文件失敗,不能創(chuàng)建臨時(shí)文件所在的目錄!");
                    return null;
                }
            }
            try ...{
                //在指定目錄下創(chuàng)建臨時(shí)文件
                tempFile = File.createTempFile(prefix, suffix, dir);
                return tempFile.getCanonicalPath();
            } catch (IOException e) ...{
                e.printStackTrace();
                System.out.println("創(chuàng)建臨時(shí)文件失??!" + e.getMessage());
                return null;
            }
        }
    }
   
    public static void main(String[] args) ...{
        //創(chuàng)建目錄
        String dirName = "D:/work/temp/temp0/temp1";
        CreateFileUtil.createDir(dirName);
        //創(chuàng)建文件
        String fileName = dirName + "/temp2/tempFile.txt";
        CreateFileUtil.createFile(fileName);
        //創(chuàng)建臨時(shí)文件
        String prefix = "temp";
        String suffix = ".txt";
        for (int i = 0; i < 10; i++) ...{
            System.out.println("創(chuàng)建了臨時(shí)文件:"
                    + CreateFileUtil.createTempFile(prefix, suffix, dirName));
        }
        //在默認(rèn)目錄下創(chuàng)建臨時(shí)文件
        for (int i = 0; i < 10; i++) ...{
            System.out.println("在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:"
                    + CreateFileUtil.createTempFile(prefix, suffix, null));
        }
    }

}
輸出結(jié)果:


創(chuàng)建目錄D:/work/temp/temp0/temp1成功!
目標(biāo)文件所在目錄不存在,準(zhǔn)備創(chuàng)建它!
創(chuàng)建單個(gè)文件D:/work/temp/temp0/temp1/temp2/tempFile.txt成功!
創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5171.txt
創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5172.txt
創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5173.txt
創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5174.txt
創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5175.txt
創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5176.txt
創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5177.txt
創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5178.txt
創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5179.txt
創(chuàng)建了臨時(shí)文件:D:work emp emp0 emp1 emp5180.txt
在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5181.txt
在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5182.txt
在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5183.txt
在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5184.txt
在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5185.txt
在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5186.txt
在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5187.txt
在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5188.txt
在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5189.txt
在默認(rèn)目錄下創(chuàng)建了臨時(shí)文件:C:Documents and SettingsAdministratorLocal SettingsTemp emp5190.txt
文章出處:http://www./course/3_program/java/javaxl/20071129/89522.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多