java 创建临时文件

11 篇文章 0 订阅

https://blog.csdn.net/makeitperfect/article/details/12975529

感谢大神的这篇博客,非常好用

一起学习,共同进步

import java.io.File;
import java.io.IOException;
 
/**
 * 创建新文件和目录
 */
public class CreateFileUtil {
 
	/**
	 * 创建单个文件
	 * @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;
		}
	}
 
	/**
	 * 创建目录
	 * @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.mkdir()) {
			System.out.println("创建目录" + destDirName + "成功!");
			return true;
		} else {
			System.out.println("创建目录" + destDirName + "失败!");
			return false;
		}
	}
 
	/**
	 * 创建临时文件
	 * @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;
			}
		}
	}
 
	public static void main(String[] args) {
		// 创建目录
		String dirName = "C:/temp/temp0/temp1";
		CreateFileUtil.createDir(dirName);
		// 创建文件
		String fileName = dirName + "/temp2/tempFile.txt";
		CreateFileUtil.createFile(fileName);
		// 创建临时文件
		String prefix = "temp";
		String surfix = ".txt";
		for (int i = 0; i < 10; i++) {
			System.out.println("创建了临时文件: "
					+ CreateFileUtil.createTempFile(prefix, surfix, dirName));
		}
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值