在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异常。




