JAVA实现单个文件,目录以及临时文件的创建工具类

来源:互联网 发布:淘宝细节图片尺寸 编辑:程序博客网 时间:2024/06/03 16:51

这个工具类主要实现的是
创建单个文件
创建目录
创建临时文件等功能
下面是具体代码


创建单个文件

/**     * 创建单个文件  createNewFile()     * @param destFileName    目标文件名     * @return 创建成功,返回true,否则返回false     */    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;        }    }

创建目录

/**      * 创建目录   dir.mkdirs(     * @param destDirName   目标目录名     * @return 目录创建成功放回true,否则返回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;        }    }

创建临时文件

/**     * 创建临时文件  createDir     * @param prefix    临时文件名的前缀     * @param suffix    临时文件名的后缀     * @param dirName   临时文件所在的目录,如果输入null,则在用户的文档目录下创建临时文件     * @return 临时文件创建成功返回true,否则返回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;            }        }    }

以上就是相关的工具代码。对于文件中的System.out.println()中的提示代码,在实际开发中可以使用日子文件输出,这样比较方便查看。

原创粉丝点击