私のアプリは音を録音します。サウンドが録音されると、ユーザーは新しいファイル名を入力するよう求められます。次にしようとしているのは、すべてのファイル名をテキスト ファイルに追加して、後でそれを配列として読み取ってリストビューを作成できるようにすることです。
これはコードです:
//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();
}
問題は、新しいファイルを記録するたびに、前の行が上書きされることです。したがって、2 つのファイル 'text1' と 'text2' を記録すると、text1 を記録した後、txt ファイルには text1 が表示されますが、text2 を記録した後、text2 は新しい行を挿入する代わりに text1 を上書きします。
追加してみました:
bw.NewLine()
前
bw.write(">" + content + ".mp3");
しかし、うまくいきません。
3 つのサウンドを録音して、sound1、sound2、sound3 という名前を付けた場合、結果は次のようになります。
>sound1
>sound2
>sound3