BufferedReader input = new BufferedReader (new FileReader("data.txt")); //Reading the file
String data [] = readFile (input); //data is each line in the file
String student [] = new String [10];
for(int x = 0; x<data.length; x++)
{
student = data[x].split(","); //each line is being split into 11 parts
}
我需要在不覆盖它的情况下写入此文件。我问了10个问题,比如“你的名字是什么?”,以及“你的姓氏是什么?”。我需要将这些问题的答案带入学生[]。就像我说的,我需要代码写入这个文件而不会覆盖它。
答案 0 :(得分:2)
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter("outfilename",
true)));
out.println("the text");
} catch (IOException e) {
e.printStackTrace ();
}
finally {
if (out != null) {
out.close ();
}
}
FileWriter构造函数的第二个参数将告诉它附加到文件(而不是清除文件)。