package com.oracle.cl;
import java.io.File;
import java.io.IOException;
public class CreateFile
{
public static void main(String[] args) throws IOException
{
String path="D:\\hello1\\hello2\\hello3\\hello.txt";
File f=new File(path);
String directory = path.replace("\\"+f.getName(), "");
File f2=new File(directory);
// 创建文件,一定要先有目录
// 创建目录
if(!f2.exists())
{
f2.mkdirs();
}
// 创建文件
if(!f.exists())
{
f.createNewFile();
}
}
}
运行结果: