2013-08-08 4 views
1

내 응용 프로그램 소리가납니다. 사운드가 녹음되면 새 파일 이름을 입력하라는 메시지가 나타납니다. 이제 내가하려는 것은 텍스트 파일에 모든 파일 이름을 추가하여 나중에 목록 뷰로 만들기위한 배열로 읽을 수 있도록하는 것입니다. 텍스트 파일에 새 텍스트 줄 추가

코드입니다 :

//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 
+0

가능한 중복 교체 : http://stackoverflow.com/a/5987040/2500326 예상대로 – Sw4Tish

답변

4

사용 FileWriter 생성자

FileWriter fw = new FileWriter(recordedFiles.getAbsoluteFile(), true); 
//                ^^^^ 

이 파일을하게됩니다 인수 boolean append에 : 나는 세 가지 소리를 녹음하고 그들에게 Sound1 (사운드 1), sound2 및 sound3 이름을 경우

, 나는이 결과가되고 싶어요 이전 내용을 덮어 쓰지 말고 끝에 텍스트를 추가하십시오.

+0

작품을, 감사합니다! – Pizzret

0

당신은 그것을 덮어 쓰기하는 경우는 line.separatorProperty

bw.write(">" + content + ".mp3"); 
bw.write(System.getProperty("line.separator").getBytes()); 
0

FileWriter 부울 소요 추가하여 그것을 할 수 있습니다. 덮어 쓰기 대신 파일에 추가하려면 true을 사용하십시오.

0

FileWriter fw = new FileWriter(recordedFiles.getAbsoluteFile()); 

대신

FileWriter fw = new FileWriter(recordedFiles.getAbsoluteFile(), true); 
+0

이것을 확인 했습니까? 내 코드에서 작동했습니다. –