Dec
8
如同前面说的使用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) {
}
}
May 22nd, 2009 at 4:44
引用read或write等子函数时,context写什么?如:String c=read(?,?),?处写什么?
August 20th, 2009 at 10:14
有FileInputStream和FileOutputStream,以前java怎么用android就怎么用了.
November 28th, 2009 at 12:06
写得一点都不全面,file 参数写什么
March 5th, 2010 at 4:43
jLPDxO http://jfkIj4nC01mcKkavYstU3l.net
April 6th, 2010 at 1:19
А телефон свой не оставите? Хотелось бы кое-что обсудить по теме.
May 18th, 2010 at 11:57
Hello! baedcbb interesting baedcbb site!
May 18th, 2010 at 11:58
Very nice site! cheap viagra
May 18th, 2010 at 11:58
Very nice site! [url=http://aixopey.com/qqatat/2.html]cheap cialis[/url]
May 19th, 2010 at 12:00
Very nice site! cheap cialis http://aixopey.com/qqatat/4.html
May 19th, 2010 at 12:00
Very nice site!
May 21st, 2010 at 8:12
Hello!
buy cialis ,
May 21st, 2010 at 8:12
Hello!
cialis ,
May 24th, 2010 at 3:47
Hello!
cialis ,
May 25th, 2010 at 5:14
Hello!
viagra ,
May 26th, 2010 at 4:21
Hello!
cialis ,
May 27th, 2010 at 4:04
Hello!
viagra ,
May 28th, 2010 at 7:15
Hello!
buy cialis ,
May 30th, 2010 at 2:00
Hello!
viagra ,
June 2nd, 2010 at 12:41
Hello!
cheap cialis ,
June 23rd, 2010 at 2:16
so true. it’s our dreams that keep us keeping on.
June 28th, 2010 at 9:57
http://www.roiding.com is very the most informative. I enjoyed your blog a lot. Thank you.
July 7th, 2010 at 3:10
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/
July 11th, 2010 at 1:21
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
July 26th, 2010 at 8:44
mQIHahpl