【发布时间】:2020-12-11 01:29:45
【问题描述】:
您好,我正在尝试将用户输入附加到文件中,直到用户输入退出字符。我已经达到了一定程度,但我遇到的问题是退出字符也附加在文本文件中。 以下是有关该问题的一些代码。 提前致谢。
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter writer = null;
BufferedReader reader = null;
try {
writer = new BufferedWriter(new FileWriter("C:\\Users\\gg\\newfile.txt",true));
Scanner input = new Scanner(System.in);
System.out.println("Enter text to enter file: ");
//String text = input.nextLine();
String text = "";
while(!text.equals("/")){
text = input.nextLine();
writer.write(text);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
writer.close();
}
}
【问题讨论】: