文件夹与文件的建立条件关系

文件夹与文件的建立条件关系



public class FileTest {
	public static void main(String[] args) {

	String path = "D:\\1\\2";
		File file = new File(path);
		System.out.println(file.exists());
		System.out.println(file.isDirectory());
		file.mkdir();// 创建当前文件夹,如果上一级文件夹不存在,则创建失败并且不报异常;反之,则创建成功;如果该文件夹已存在,则不创建新的文件夹对已有的进行覆盖。
		System.out.println(file.exists());
		System.out.println(file.isDirectory());
	
	

	String path = "D:\\1\\2";
		File file = new File(path);
		System.out.println(file.exists());
		System.out.println(file.isDirectory());
		file.mkdirs();// 创建多层文件夹,不论祖先级文件夹是否存在,都创建成功;如果祖先级文件夹不存在,则一并创建,如果该文件夹已存在,则不创建新的文件夹对已有的进行覆盖。
		System.out.println(file.exists());
		System.out.println(file.isDirectory());



	String path = "D:\\test.txt";
		File file = new File(path);
		System.out.println(file.exists());
		System.out.println(file.isDirectory());
		try {
			file.createNewFile();// 在指定文件夹下创建文本文件test.txt,如果祖先级文件夹不存在,则创建失败,并报异常。
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(file.exists());
		System.out.println(file.isDirectory());



	String path = "D:\\1\\2";
		File file = new File(path);
		File tempFile = null;
		try {
			tempFile = file.createTempFile("hello", ".png");// 不论祖先级文件夹是否存在,都创建成功,但是磁盘里找不到它的影子。
			System.out.println(tempFile.exists());// true
			System.out.println(tempFile.isDirectory());// false
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(tempFile.exists());// true
		System.out.println(tempFile.getAbsolutePath());// C:\Users\ADMINI~1\AppData\Local\Temp\hello6892397912518121668.png
		System.out.println(tempFile.getPath());// C:\Users\ADMINI~1\AppData\Local\Temp\hello6892397912518121668.png
		System.out.println(tempFile.getName());// hello6892397912518121668.png,中间是随机数字字符
		System.out.println(tempFile.isDirectory());// false



	String path = "D:\\1";
		File file = new File(path);
		File tempFile = null;
		try {
			tempFile = file.createTempFile("hello", ".png", file);// 祖先级文件夹不存在则创建失败并报异常,反之则创建成功,并且磁盘里也可以找到该文件。
			System.out.println(tempFile.exists());// true
			System.out.println(tempFile.isDirectory());// false
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(tempFile.exists());// true
		System.out.println(tempFile.getAbsolutePath());// D:\1\hello4749175659870866788.png
		System.out.println(tempFile.getPath());// D:\1\hello4749175659870866788.png
		System.out.println(tempFile.getName());// hello4749175659870866788.png,中间是随机数字字符
		System.out.println(tempFile.exists());// true
		System.out.println(tempFile.isDirectory());// false
	}
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值