我是Android编程的新手,如果这个问题听起来很愚蠢,请原谅。我想从Android程序中的文件中读取和写入。我知道如何在Java中做到这一点。
在阅读完文档后,我编写了以下代码。但是,我找不到这个文件的位置。根据文档,它应该在文件夹data/data/package_name中。
我有三星Galaxy S2。我启动了应用程序,然后将其关闭。当我去找那个文件的时候,我找不到它。我查看了我的文件/数据。但是这个数据文件夹中没有数据文件夹。我做错了什么?
谢谢你的帮助。
资料来源: 1. http://developer.android.com/training/basics/data-storage/files.html 2. http://www.vogella.com/articles/AndroidFileBasedPersistence/article.html#overview
public class MainActivity extends Activity {
private void writeFileToInternalStorage() {
String eol = System.getProperty("line.separator");
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(openFileOutput("myfile.txt", MODE_WORLD_WRITEABLE)));
writer.write("This is a test1." + eol);
writer.write("This is a test2." + eol);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
writeFileToInternalStorage();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}发布于 2012-12-16 18:46:11
除非你的手机是根的,或者你正在使用一个模拟器,否则我不认为你将能够看到私有文件。无论如何,您可以在模拟器上执行"adb shell“,然后执行"cd /data/data/package_name/”来查看文件。
一个不推荐使用的选项是在openFileOutput(String name,int mode)函数的模式部分将标志设置为MODE_WORLD_READABLE或MODE_WORLD_WRITEABLE。
否则,如果您想通过您的文件管理器访问它,您始终可以选择将非私人数据存储在手机的外部存储器上。请看一下Save a File on External Storage部分,了解如何做到这一点。
发布于 2012-12-16 18:43:51
您可以使用getExternalFilesDir获取SDCard上可以写入文件的目录。使用null作为通用文件的参数。
路径为/mnt/sdcard/Android/data/package/files
发布于 2012-12-16 18:37:15
openFileOutput()函数创建您的应用程序可访问的私有文件。与SharedPreferences文件(私有文件)相同。我的意思是,就像首选项文件是隐藏的,但应用程序可以访问,就像您的文件隐藏并存储在私有内存中一样。您可以从代码访问,但不能使用文件查看器访问。
https://stackoverflow.com/questions/13900356
复制相似问题