java创建文件和目录

创建文件和目录的重点如下:

1、File类的createNewFile根据抽象路径创建一个新的空文件,当抽象路径指定的文件存在时,创建失败

2、File类的mkdir方法根据抽象路径创建目录

3、File类的mkdirs方法根据抽象路径创建目录,包括创建必需但不存在的父目录

4、File类的createTempFile方法创建临时文件,可以指定临时文件的文件名前缀、后缀及文件所在的目录,如果不指定目录,则存放在系统的临时文件夹下。

5、除mkdirs方法外,以上方法在创建文件和目录时,必须保证目标文件不存在,而且父目录存在,否则会创建失败

实例如下

package jijian.test;

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

public class CreateFileUtil {
	
	public static boolean createFile(String destFileName) {
		File file = new File(destFileName);
		
		if (file.exists()) {
			System.out.println("创建单个文件" + destFileName + "失败,目标文件已存在!");
			return false;
		}
		
		//关于File.separator:windows是\,unix是/
		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) {
			// TODO Auto-generated catch block
			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()) {
			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 (null == dirName) {
			//在默认文件夹下创建临时文件
			try {
				tempFile = File.createTempFile(prefix, suffix);
				//canonical 英[kəˈnɒnɪkl] 美[kəˈnɑnɪkəl] 权威的
				return tempFile.getCanonicalPath();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				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) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("创建临时文件失败!" + e.getMessage());
				return null;
			}
			
		}
	}
	
	public static void main(String[] args) {
		//创建目录,在D盘根目录下创建,注意特殊字符\的转义
		//注意UNIX/Linux系统下路径符是正斜杠/
		String dirName = "D:\\temp\\temp1";
		CreateFileUtil.createDir(dirName);
		//创建文件
		String fileName = dirName + "\\temp2\\tempFile.txt";
		CreateFileUtil.createFile(fileName);
		//创建临时文件
		String prefix = "temp";
		String suffix = ".txt";
		for (int i = 0; i < 10; i++) {
			System.out.println("创建了临时文件:"
					+ CreateFileUtil.createTempFile(prefix, suffix, dirName));
		}
		//在默认目录下创建临时文件
		for (int i = 0; i < 10; i++) {
			System.out.println("在默认目录下创建了临时文件:"
					+ CreateFileUtil.createTempFile(prefix, suffix, null));
		}
	}
}

结果如下

 


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