Android文件IO接口类 [英] Android File IO Interface Class
问题描述
我在File IO的API上做了一些阅读,阅读以下博客文章: http://techknock.blogspot.com/2008/05/file-hadling-in-android.html 。当所有事情都在同一个活动中时,他的代码工作正常。但是我想要做的是创建一个IOInterface类,我可以使用它来打开多个数据库来填充多个列表。
ListA.java
public class ListA
{
public List< ClassA>列表;
private final String DBA =dbA;
私有IOInterface数据库;
$ b $ public List()
{
list = new ArrayList< ClassA>();
database = new IOInterface();
$ b public void initListA()throws IOException
{
database.openForWriting(DBA);
String myStr = new String(content);
database.dos.writeBytes(myStr);
database.dos.flush();
database.dos.close();
IOInterface.java / p>
public class IOInterface
{
public DataOutputStream dos;
私有FileOutputStream fos;
$ b $ public void openForWriting(String database)
{
try {
fos = openFileOutput(database,Content.MODE_PRIVATE);
dos = new DataOutputStream(fos);
} catch(FileNotFoundException e){
e.printStackTrace();
Eclipse下划线 fos = openFileOutput(database,Content.MODE_PRIVATE);
。注释 openFileOutput()
不存在。解决方法是将IOInteface类作为Activity来扩展。我想,然后 openFileOutput()
是一个活动类的方法。
所以我的问题是如何做到我正在努力?标准Java文件io如:
文件fp = new File(database);
FileOutputStream fos = new FileOutputStream(fp);
不起作用。它捕获一个FileNotFoundException。这必须是可行的。任何想法?
谢谢,
解决方案 c> openFileOutput()包含在 Context
类中,因此可以将此类的实例传递给打开文件的方法。你应该总是使用来自 Context
的方法来处理文件。
你可以阅读关于使用内部和外部存储在开发指南中。
I did some reading on the API for File IO and read the following blog post: http://techknock.blogspot.com/2008/05/file-hadling-in-android.html. His code works fine when everything is in the same activity. But what I am trying to do is create an IOInterface class that I can use to open multiple databases to populate multiple lists.
ListA.java
public class ListA
{
public List<ClassA> list;
private final String DBA = "dbA";
private IOInterface database;
public List()
{
list = new ArrayList<ClassA>();
database = new IOInterface();
}
...
public void initListA() throws IOException
{
database.openForWriting(DBA);
String myStr = new String("content");
database.dos.writeBytes(myStr);
database.dos.flush();
database.dos.close();
}
}
IOInterface.java
public class IOInterface
{
public DataOutputStream dos;
private FileOutputStream fos;
public void openForWriting(String database)
{
try {
fos = openFileOutput(database, Content.MODE_PRIVATE);
dos = new DataOutputStream(fos);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Eclipse underlines fos = openFileOutput(database, Content.MODE_PRIVATE);
. With the comment that openFileOutput()
does not exist. The resolution to this is to extend the IOInteface class as an Activity. I suppose then openFileOutput()
is a method of an activity class.
So my question is how do accomplish what I am trying to do? Standard Java file io such as:
File fp = new File("database");
FileOutputStream fos = new FileOutputStream(fp);
does not work. It catches a FileNotFoundException. This has to be doable though. Any ideas?
Thanks,
解决方案 Method openFileOutput()
is contained in the Context
class, so you can pass an instance of this class to your method that opens files. And you should always use methods from Context
when you want to work with files.
You can read about using the internal and external storages in the development guide.
这篇关于Android文件IO接口类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文