当我在 Android 模拟器上创建并尝试写入文件时,为什么会收到 FileNotFoundException?
首先,我不想写入 SDCard。我想将一些信息写入在应用程序使用期间保留的文件中。它本质上是一个保存特定用户收藏夹的文件。代码如下所示:
try {
File file = new File("favorites.txt");
if (file.exists()) {
Log.d(TAG, "File does exist.");
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis));
}
else {
Log.d(TAG, "File does not exist.");
return favDests;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
运行此代码时,我们总是得到“文件不存在”。我们的 DDMS 日志中的消息。
我们还尝试了以下代码,但无济于事:
try {
File file = new File(GoLincoln.FAV_DEST_FILE);
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
正是代码的第二部分导致了 FileNotFoundException。
我已经阅读了多个关于在 Android 上编写和读取文件的教程,并且我相信我非常密切地关注它们,所以我不确定为什么这段代码不能成功运行。我很感激任何帮助!
First off, I am not trying to write to the SDCard. I want to write some information to a file that persists between uses of the app. It is essentially a file to hold favorites of the particular user. Here is what the code looks like:
try {
File file = new File("favorites.txt");
if (file.exists()) {
Log.d(TAG, "File does exist.");
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis));
}
else {
Log.d(TAG, "File does not exist.");
return favDests;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
When running this code, we always get the "File does not exist." message in our DDMS log.
We have also tried the following code to no avail:
try {
File file = new File(GoLincoln.FAV_DEST_FILE);
fis = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(fis));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
It is this second portion of code that results in the FileNotFoundException.
I have read multiple tutorials on writing and reading files on Android and I believe I am following them pretty closely, so I am not sure why this code doesn't work successfully. I appreciate any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应直接使用
File
类。使用Activity.getCacheDir()
获取特定于您的应用程序的缓存目录。然后使用 new File(cachedir, "filename.tmp") 来创建文件。You shouldn't use the
File
class directly. UseActivity.getCacheDir()
to get the cache dir which is specific to your application. Then usenew File(cachedir, "filename.tmp")
to create the file.Preferences 和 SQLLite 都允许您拥有持久数据,而无需管理自己的文件。
要使用共享首选项,您可以从上下文中获取它,然后像这样编辑值。
获取首选项
这实际上是执行基本首选项的最简单方法,比创建文件容易得多。除了支持字符串之外,还可以将大多数基本数据类型添加到 Preferences 类中。
Preferences and SQLLite will both allow you to have persistent data without managing your own files.
To use shared preferences you grab it from your context, then you edit the values like so
To get a preference out
This is really the simplest way to do basic preferences, much easier then doing a file. More then strings are supported, most basic data types are available to be added to the Preferences class.