这就是我正在做的事情:
static void writeToFile(String text) {
try {
synchronized (fileLock) {
File external = Environment.getExternalStorageDirectory();
String sdcardPath = external.getPath();
FileWriter filewriter = new FileWriter(sdcardPath
+ "/Documents/myfile.txt", true);
BufferedWriter out = new BufferedWriter(filewriter);
out.write(text);
out.close();
filewriter.close();
}
} catch (Exception e) {
android.util.Log.d("failed to save file", e.toString());
}
}
在打开的文件中,我得到以下异常:
java.io.FileNotFoundException: /mnt/sdcard/Documents/myfile.txt:
open failed: ENOENT (No such file or directory)
- 该目录
/mnt/sdcard/Documents/
根据Root Browses存在 - 应用程序清单确实包含
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
myfile.txt
不存在,我希望创建或附加它
如何在 android 上创建或附加到文件?
更新 这是我的 AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>