Java创建文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38078822/article/details/81879443

 定义String类型的strPath,该文件夹可以存在也可不存在,然后定义File类型,将strPath放入其中,最后做判断,如果该文件夹不存在则按照strPath类型创建文件夹,反之则反。

	String strPath = "d:\\file\\a";
	File file = new File(strPath);
	if(!file.exists()){
		file.mkdirs();
	}

如果想实现创建新的文件夹且创建同时在该文件夹下创建一个文件,那么我们就如下操作

package image;

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

public class test {
public static void main(String[] args) throws IOException {
 String strPath="d:/file/a/a.txt";
 File file = new File(strPath);
 File fileParent = file.getParentFile();
 if(!fileParent.exists()){
	 fileParent.mkdirs();
 }
 file.createNewFile();
	
}
}

猜你喜欢

转载自blog.csdn.net/qq_38078822/article/details/81879443