public class Main {
public static void main(String[] args) {
try {
createFile();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
private static void createFile() throws IOException {
String folder = "/a/b/001.txt";
File apkFile = new File(folder);
if (!apkFile.getParentFile().exists()) {
if (!apkFile.getParentFile().mkdirs()) {
throw new IOException("创建多级目录失败");
}
}
if (!apkFile.exists()) {
if (!apkFile.createNewFile()) {
throw new IOException("创建文件失败");
}
} else {
if (!apkFile.delete()) {
throw new IOException("删除文件失败");
}
if (!apkFile.createNewFile()) {
throw new IOException("创建文件失败");
}
}
}
}
Java创建一个包含多级目录的空文件
最新推荐文章于 2021-02-16 14:47:06 发布