2
我写了一个程序,允许用户在一个文件中存储多个备忘录。我想出了如何在Java中使用PrintWriter &文件,但是我的问题与我的输出有关。当我在记事本中检查文件时,我只能输入一个备忘录,没有问题&,只有一个备忘录存在。下面的代码:PrintWriter&File in java
import java.util.*;
import java.io.*;
public class MemoPadCreator{
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
boolean lab25 = false;
File file = new File("revisedLab25.txt");
PrintWriter pw = new PrintWriter (file);
String answer = "";
do{
while(!lab25){
System.out.print("Enter the topic: ");
String topic = input.nextLine();
Date date = new Date();
String todayDate = date.toString();
System.out.print("Message: ");
String memo = input.nextLine();
pw.println(todayDate + "\n" + topic + "\n" + memo);
pw.close();
System.out.print("Do you want to continue(Y/N)?: ");
answer = input.next();
}
}while(answer.equals("Y") || answer.equals("y"));
if(answer.equals("N") || answer.equals("n")){
System.exit(0);
}
}
}
下面是输出:
Enter the topic: I love food!
Message: Food is life!
Do you want to continue(Y/N)?: Y
Enter the topic: Message:
如何去改变它,以便输出可以让我继续储存的备忘录,直到我告诉它停下来?
什么是确切的问题?程序的两次运行之间是否会覆盖旧的文件内容?这是因为PrinteWriter覆盖文件,请参阅https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#PrintWriter%28java.io.File%29或者您的问题是关于其他问题? – Robert
罗伯特 - 我的文件总是被覆盖,但我们应该在文件中存储多个备忘录。 –