简体   繁体   English

Android App内部数据存储

[英]Android App Internal Data Storage

I am attempting to save an array of longs as a text file using the Android's internal memory, and then access that array in a different activity. 我试图使用Android的内部存储器将long数组保存为文本文件,然后在其他活动中访问该数组。 After noticing that was broken, I isolated the problem in this chunk of code below. 注意到已损坏后,我在下面的这段代码中隔离了该问题。

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();
}

The text field that is set to display what is read from the file has the following result: 设置为显示从文件中读取的内容的文本字段具有以下结果:

[[,2,0,0]

The result I expected was the original array from above. 我期望的结果是上面的原始数组。 What am I doing wrong here? 我在这里做错了什么? Any help or advice would be appreciated. 任何帮助或建议,将不胜感激。

Replace this : 替换为:

        InputStreamReader isReader = new InputStreamReader(fis);
        char[] buffer = new char[timings.length];
        isReader.read(buffer);

        textField.setText(Arrays.toString(buffer));

with this : 有了这个 :

        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);

I hope it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM