如同前面说的使用Preferences一样,使用File来读写文件也属于常规思路。在Android中没有提供像J2SE里面多的让人抓狂的有关于IO的API。所以使用起来非常简单轻巧。

在Android系统中,这些文件保存在 /data/data/PACKAGE_NAME/files 目录下。

数据读取

public static String read(Context context, String file) {
String data = "";
try {
FileInputStream stream = context.openFileInput(file);
StringBuffer sb = new StringBuffer();
int c;
while ((c = stream.read()) != -1) {
sb.append((char) c);
}
stream.close();
data = sb.toString();

} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return data;
}

从代码上,看起来唯一的不同就是文件的打开方式了: context.openFileInput(file); Android中的文件读写具有权限控制,所以使用context(Activity的父类)来打开文件,文件在相同的Package中共享。这里的 Package的概念同Preferences中所述的Package,不同于Java中的Package。

数据写入

public static void write(Context context, String file, String msg) {
try {
FileOutputStream stream = context.openFileOutput(file,
Context.MODE_WORLD_WRITEABLE);
stream.write(msg.getBytes());
stream.flush();
stream.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}

在这里打开文件的时候,声明了文件打开的方式。

一般来说,直接使用文件可能不太好用,尤其是,我们想要存放一些琐碎的数据,那么要生成一些琐碎的文件,或者在同一文件中定义一下格式。其实也可以将其包装成Properties来使用:

public static Properties load(Context context, String file) {
Properties properties = new Properties();
try {
FileInputStream stream = context.openFileInput(file);
properties.load(stream);
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
return properties;
}

public static void store(Context context, String file, Properties properties) {
try {
FileOutputStream stream = context.openFileOutput(file,
Context.MODE_WORLD_WRITEABLE);
properties.store(stream, "");
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}

25 Responses to “Android数据存取之Files”

  1. 引用read或write等子函数时,context写什么?如:String c=read(?,?),?处写什么?

  2. 有FileInputStream和FileOutputStream,以前java怎么用android就怎么用了.

  3. 写得一点都不全面,file 参数写什么

  4. jLPDxO http://jfkIj4nC01mcKkavYstU3l.net

  5. А телефон свой не оставите? Хотелось бы кое-что обсудить по теме.

  6. Hello! baedcbb interesting baedcbb site!

  7. Very nice site! cheap viagra

  8. Very nice site! [url=http://aixopey.com/qqatat/2.html]cheap cialis[/url]

  9. Very nice site! cheap cialis http://aixopey.com/qqatat/4.html

  10. Very nice site!

  11. Hello!
    buy cialis ,

  12. Hello!
    cialis ,

  13. Hello!
    cialis ,

  14. Hello!
    viagra ,

  15. Hello!
    cialis ,

  16. Hello!
    viagra ,

  17. Hello!
    buy cialis ,

  18. Hello!
    viagra ,

  19. Hello!
    cheap cialis ,

  20. so true. it’s our dreams that keep us keeping on.

  21. http://www.roiding.com is very the most informative. I enjoyed your blog a lot. Thank you.

  22. Tucked away in our timberland for you subconscious is an idyllic vision. We see ourselves on a long trip that timberland 6 inch spans the continent. We are traveling by train. Out timberland hiking boots windows, we drink in the passing scene of cars on nearby highways, of children timberland shoe company waving at a crossing, of cattle grazing on a distant timberland boots hillside, of smoke pouring from a power plant, of row upon row of corn and wheat, of flatlands and timberland wheat shoes valleys, of mountains and rolling classic 3 eye timberland boat hillsides, of city skylines and village halls.But uppermost in our black timberland boots minds is the final destination. On a certain cheap timberland boots day at a certain hour, we will pull into the station. Bands will be playing and flags discount timberland boots waving. Once we get there, so many wonderful timberland winter boots dreams will come true and the pieces of our lives will fit together like a completed jigsaw puzzle. How restlessly we pace the aisles, womens timberland boot the minutes for loitering –waiting, waiting, waiting for the station.But uppermost in our timberland shoes store minds is the final destination. On a certain day at a certain hour, we will pull into the station. Bands will be playing and timberland eye boat flags waving. http://www.timberland6inch.com/

  23. http://www.roiding.com is great! Payday loans are very short term normally you have to pay them back on your very net payday Payday loans are not advisable especially if you feel that you cannot afford to lose your paycheck the following week

  24. mQIHahpl

Trackbacks/Pingbacks

  1. Android 数据存取之Files « Emck

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>