我是Android开发的新手.我尝试写一些文件.我正在使用代码:
try {
FileOutputStream fOut = openFileOutput(
"out.txt", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write("something");
osw.flush();
osw.close();
fOut.close();
} catch (MalformedURLException e) {
Log.e("FILE WRITE ERROR", e.getMessage());
} catch (IOException e) {
Log.e("FILE WRITE ERROR", e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
一切都没问题,但我在DDMS FIle Explorer中找不到该文件.你能帮助我并告诉我Android保存文件的位置吗?它不在"/ data/data/my_project_package"中.谢谢.
1)一定要在你的清单中有这个:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)
2)无论是使用仿真器还是使用设备进行测试都会产生影响.如果是后者,请务必从PC上卸下设备(选择:"仅充电").调试仍然会以这种方式工作.只是你不能同时写作和观看.
此方法有效:
public void writeToExternalStoragePublic(String filename, int content) {
String packageName = this.getPackageName();
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/Android/data/" + packageName + "/files/";
if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) {
try {
boolean exists = (new File(path)).exists();
if (!exists) {
new File(path).mkdirs();
}
// Open output stream
FileOutputStream fOut = new FileOutputStream(path + filename,true);
// write integers as separated ascii's
fOut.write((Integer.valueOf(content).toString() + " ").getBytes());
fOut.write((Integer.valueOf(content).toString() + " ").getBytes());
// Close output stream
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11800 次 |
| 最近记录: |