2015-04-01 99 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" />