public void writeToFile(){
try{
String data = "\n" +" This content will append to the end of the file";
File file =new File("questions.txt");
//if file doesnt exists, then create it
if(!file.exists()){
file.createNewFile();
}
//true = append file
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write(data);
bufferWritter.close();
System.out.println("Done");
}catch(IOException e){
e.printStackTrace();
}
}
我上面有以下代码,这是从互联网上取下的,我能够正确地将数据添加到指定的文件中,但是我需要这样做以便在新行上添加数据。而不是追加到当前的最后一行
答案 0 :(得分:2)
尝试BufferedWriter.newLine()
方法。