我正在尝试通过询问用户所说的信息来更新包含电影和一些信息列表的.txt文件。 该文件的组织方式如下:
0000-Movie Title Goes Here-9.99-ClientID
0001-Same thing here-9.99-CLIENTID2
等等。
这是我正在使用的代码:
BufferedWriter output = new BufferedWriter(new FileWriter("movies.txt", true));
System.out.println("Inserte numero de la pelicula:\n");
String pelNum = CTeclado.ReadString();
System.out.println("Inserte el titulo de la pelicula:\n");
String pelName = CTeclado.ReadString();
System.out.println("Inserte el precio de la pelicula:\n");
String pelPrice = CTeclado.ReadString();
output.append(pelNum + "-" + pelName + "-" + pelPrice + "-null");
output.close();
ReadM();
System.out.println("Pelicula agregada!\n");
MainMenu();
(CTeclado用于读取输入)
这编译没有错误,但实际文件没有变化....
我的代码出了什么问题?
答案 0 :(得分:1)
我认为您应该尝试下面的代码
PrintStream output = new PrintStream(new File("movies.txt"));
output.append(pelNum + "-" + pelName + "-" + pelPrice + "-null").append("\n");
答案 1 :(得分:0)
使用output.write("\n" + "stuff goes here)
就像@ fireshadow52说的那样
output.append("\n" + "stuff")
也有效,必须确保.txt文件正好在行的末尾结束(不在新行上)