java 創建臨時文件

原文鏈接:https://blog.csdn.net/makeitperfect/article/details/12975529

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));
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章