2012-08-15 6 views
0

동적으로 txt 파일에 쓰려고합니다. 파일 1 줄을 만들고 쓸 수 있습니다. 그 후 두 번째 줄을 쓰고 싶습니다. 아래 코드에서 파일이 존재하는지 확인합니다. 파일이 이미 존재하는 경우 새 파일을 만들지 않고 파일에 씁니다 (아래 코드 참조). 그러나 내가 시도하고 두 번째로 쓸 때마다이 오류가 발생합니다.Android에서 동적으로 txt 파일 업데이트

오류 :

.IllegalArgumentException: File //sdcard//uiu_error_report.txt contains a path separator 

코드 :이 작업을 수행하는

String filename = "/sdcard/uiu_error_report.txt"; 

File myFile = new File(filename); 

if (myFile.createNewFile()) { 

    FileOutputStream fOut = new FileOutputStream(myFile); 
    OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); 
    myOutWriter.append("[" + dateFormatted + "," + module2 + "," + type2 + "," + message2 + "]"); 
    myOutWriter.close(); 
    fOut.close(); 

} else { 

    try { 
     OutputStreamWriter out = new OutputStreamWriter(context.openFileOutput(filename,countFileRows()+1)); 
     // write the contents on mySettings to the file 
     out.write("[" + dateFormatted + "," + module2 + "," + type2 + "," + message2 + "]"); 
     // close the file 
     out.close(); 

    // Do something if an IOException occurs. 
    } catch (java.io.IOException e) { 

    } 
} 
+0

어쩌면이를 참조하십시오 http://stackoverflow.com/questions/5963535/java-lang-illegalargumentexception-a-path-separator –

답변

0

더 나은 방법 ....

try { 
    FileOutputStream fos = context.openFileOutput(filename, Context.MODE_APPEND | Context.MODE_WORLD_READABLE); 
    OutputStreamWriter myOutWriter = new OutputStreamWriter(fos); 
    myOutWriter.append("[" + dateFormatted + "," + module2 + "," + type2 + "," + message2 + "]"+"/n"); 
    // myOutWriter.close(); 

    myOutWriter.close(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

캐칭 예외는 권장되지 않습니다. 차라리 특정 오류를 포착해야합니다. – Zorayr