2015-04-01 101 views
-3
final String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Slots/"; 
    final File file = new File(rootPath); 
    file.mkdirs(); 
    final File sdFile = new File(file, "Profile.txt"); 
try { 
    BufferedWriter bw = new BufferedWriter(new FileWriter(sdFile)); 
    bw.write(edit.getText().toString() + "\n"); 
    bw.write("5000"); 
    } catch (IOException e) { 
} 

在此代碼中捕獲IOException,但文件已創建。請幫助!感謝所有的答案。寫入文件。 Java的。 Android

+1

那麼什麼異常說呢?文件中包含什麼內容? – 2015-04-01 16:18:23

+0

請詳細解釋並嘗試使用代碼示例編寫更清晰的問題 – 2015-04-01 16:20:29

回答

0

試試這個:

final String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Slots/"; 
final File file = new File(rootPath); 
file.mkdirs(); 
final File sdFile = new File(file, "Profile.txt"); 
try { 
     FileWriter fw = new FileWriter(sdFile); 
     BufferedWriter bw = new BufferedWriter(fw); 
     bw.write("hello "+"\n"); 
     bw.write("5000"); 
     bw.close(); 
    } catch (IOException e) { 
     Log.e("exception ",e.getMessage()); 
    } 

確保您在您的清單文件中添加此權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />