Java保存/打开文件对象

bluesky1年前 ⋅ 263 阅读

ChatGPT的回答仅作参考:

保存文件对象:

```java
File file = new File("path/to/file.txt");
try {
    FileWriter writer = new FileWriter(file);
    writer.write("Hello World!");
    writer.close();
} catch (IOException e) {
    e.printStackTrace();
}
```

打开文件对象:

```java
File file = new File("path/to/file.txt");
try {
    Scanner scanner = new Scanner(file);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        System.out.println(line);
    }
    scanner.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
```

全部评论: 0

    相关推荐