我正在尝试使用Android的内部存储器将一组longs保存为文本文件,然后在另一个活动中访问该数组。在注意到它被破坏之后,我在下面这段代码中找出了问题。
long[] timings = {200, 400, 600, 1400};
try {
FileOutputStream fos = openFileOutput("timings", Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(Arrays.toString(timings));
osw.close();
FileInputStream fis = openFileInput("timings");
InputStreamReader isReader = new InputStreamReader(fis);
char[] buffer = new char[timings.length];
isReader.read(buffer);
textField.setText(Arrays.toString(buffer));
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
设置为显示从文件中读取内容的文本字段具有以下结果:
[[,2,0,0]
我预期的结果是上面的原始数组。我在这做错了什么?任何帮助或建议将不胜感激。
答案 0 :(得分:1)
替换它:
InputStreamReader isReader = new InputStreamReader(fis);
char[] buffer = new char[timings.length];
isReader.read(buffer);
textField.setText(Arrays.toString(buffer));
用这个:
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String sTemp1 = "", sTemp2 = "";
while ((sTemp1 = br.readLine()) != null) {
sTemp2 = sTemp1;
}
in.close();
textField.setText(sTemp2);
我希望它会对你有所帮助。