我正在尝试在http://developer.android.com/guide/topics/data/data-storage.html
中实施“使用内部存储”部分我认为我正确地完成了第一部分,将字符串保存到文件中,但是如何在以后读取字符串?
她是我的代码:
String FILEPROFILE = "profileinfo";
FileOutputStream fos = null;
BufferedInputStream fis = null;
OutputStream out = null;
try {
fos = openFileOutput(FILEPROFILE, Context.MODE_PRIVATE);
fos.write(profile.toString().getBytes());
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis = new BufferedInputStream(new FileInputStream(FILEPROFILE));
Log.d("UsersThoughts", "BufferedInputStream is " + fis.read());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
编辑: 我把它改成了这个:
try {
fis = openFileInput(FILEPROFILE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Log.d("UsersThoughts", "This blah object read " + fis.read());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("UsersThoughts", "This blah object trying to read: " + e.toString());
}
try{
Log.d("UsersThoughts", "This blah object toString " + fis.toString());
} catch (Exception e)
{
e.printStackTrace();
Log.e("UsersThoughts", "This blah object trying to make string: " + e.toString());
}
logcat中的输出如下所示:
11-15 18:41:34.862: D/UsersThoughts(7777): This blah object read123
11-15 18:41:34.862: D/UsersThoughts(7777): This blah object toString java.io.FileInputStream@46356128
看起来它正在把文件读成叮咬......我如何获取我复制的文本?
答案 0 :(得分:0)
我引用你引用的相同来源:
从内部存储中读取文件:
调用openFileInput()并将其传递给要读取的文件的名称。这将返回一个FileInputStream。
使用read()从文件中读取字节。
- 醇>
然后用close()关闭流。
编辑:例如,
FileInputStream fis = openFileInput("FILENAME");
BufferedInputStream blah = new BufferedInputStream(fis);
然后使用此blah
对象执行您之前所做的任何操作。
答案 1 :(得分:0)
按照我在此处给出的答案将字符串存储到Android中的文件:Should I store data in sqlite in case user is offline?