在Java中,可以使用File类来创建目录和文件。以下是创建目录和文件的方法示例:
File dir = new File("path/to/directory");
if (!dir.exists()) {
dir.mkdirs();
System.out.println("目录已创建");
} else {
System.out.println("目录已存在");
}
File file = new File("path/to/file.txt");
try {
if (file.createNewFile()) {
System.out.println("文件已创建");
} else {
System.out.println("文件已存在");
}
} catch (IOException e) {
System.out.println("创建文件时出现异常");
e.printStackTrace();
}
请将上述示例中的"path/to/directory"和"path/to/file.txt"替换为实际的目录和文件路径。在创建文件时需要处理IOException异常。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:ubuntu创建目录和文件的方法是什么