我的应用程序记录声音。录制声音后,要求用户输入新的文件名。现在,我接下来要做的是将所有文件名添加到文本文件中,以便以后可以将其作为数组进行读取以创建列表视图。
这是代码:
//this is in onCreate
File recordedFiles = new File(externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/recorded files.txt");
if(!recordedFiles.exists())
{
try {
recordedFiles.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//this executes everytime text is entered and the button is clicked
try {
String content = input.getText().toString();
FileWriter fw = new FileWriter(recordedFiles.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(">" + content + ".mp3");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
现在的问题是,每当我记录一个新文件时,上一行就会被覆盖。因此,如果我记录了两个文件'text1'和'text2',则在记录text1之后,txt文件将显示text1,但是在记录了text2之后,text2将覆盖text1,而不是插入新行。
我尝试添加:
bw.NewLine()
前
bw.write(">" + content + ".mp3");
但这是行不通的。
如果我录制三个声音并将它们分别命名为sound1,sound2和sound3,则希望得到以下结果:
>sound1
>sound2
>sound3
将FileWriter
构造函数与boolean append
参数一起使用
FileWriter fw = new FileWriter(recordedFiles.getAbsoluteFile(), true);
// ^^^^
这将使文件在末尾附加文本,而不是覆盖以前的内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句