Text I/O 书上的一道习题

小公鸡卡哇伊呀~ 2018-07-26 12:52:34
(Create a text file) Write a program to create a file named Exercise17_01.txt if it does not exist. Append new data to it if it already exists. Write 100 integers created randomly into the file using text I/O. Integers are separated by a space.

我写成下面这样,但是文件存在就append该怎么改呢?PrintWriter没有类似下面的构造函数。
FileOutputStream(file: File, append: boolean)
FileOutputStream(filename: String, append: boolean)


public class Class17_1 {
public static void main(String[] args) throws IOException {
PrintWriter file = new PrintWriter("Exercise17_01.txt");
Random r = new Random();
for (int i = 0; i < 100; i++)
file.print(r.nextInt(100) + " ");
file.close();
}
}


谢谢啦
...全文
150 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
stacksoverflow 2018-07-26
  • 打赏
  • 举报
回复
PrintWriter pw = new PrintWriter(new FileOutputStream(
new File("persons.txt"),
true /* append = true */));

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧