将笔记整理到文件夹中
为了保持整洁,您的应用应将笔记保存在专用文件夹中。在本练习中,您将创建一个名为 "notes" 的目录,并打印其路径。这样可以把相关文件放在一起,便于查找。
本练习是课程的一部分
Java 中的输入/输出与流
练习说明
- 创建一个指向
notes的File对象notesDir。 - 在系统中创建名为
notes的目录。 - 获取名为
notes的目录的相对路径。
交互式实操练习
通过完成这段示例代码来试试这个练习。
class DirectoryManager {
public static void main(String[] args) {
try {
// Create a File object to represent the directory
File notesDir = ____ _____("notes");
// Create a directory in the system
if (____.____()) {
System.out.println("Directory 'notes' created successfully");
}
// Retrieve and print the relative path of the directory
System.out.println("Relative Path: " + ____.____());
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}