根据地址获取文件,不存在则创建文件及目录


先创建一级根目录

                File rootfolder = new File("C:\\Photodistinguish\\");
		// 如果文件夹不存在则创建
		if (!rootfolder.exists() && !rootfolder.isDirectory()) {
			
			rootfolder.mkdir();
			System.out.println("c盘根目录不存在");
		} else {
			System.out.println("c盘根目录存在");
		}

在创建子目录,然后创建文件

            File inputfolder = new File("C:\\Photodistinguish\\identifiedphotos\\");
		// 如果文件夹不存在则创建
		if (!inputfolder.exists() && !inputfolder.isDirectory()) {
			
			inputfolder.mkdir();
			System.out.println("//txt目录不存在");
		} else {
			System.out.println("//txt目录存在");
		}

		String filepath = "C:\\Photodistinguish\\identifiedphotos\\text.txt";
		File txtfile = new File(filepath);
		// 如果文件不存在则创建
		if (!txtfile.exists()) {
			try {
				txtfile.createNewFile();
				System.out.println("//txt目录下文件不存在");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}




猜你喜欢

转载自blog.csdn.net/weixin_39249630/article/details/80701845