【问题标题】:How to write/read array to file in android如何在android中将数组写入/读取到文件
【发布时间】:2011-03-08 03:31:15
【问题描述】:

我正在写入 android 中的文件并使用以下代码从同一个文件中读取:

//Write data on file
FileOutputStream fOut = null;

    OutputStreamWriter osw = null;

    try {

        fOut = openFileOutput("gasettings.dat", MODE_PRIVATE);


        osw = new OutputStreamWriter(fOut);

        osw.write(data);

        osw.flush();

        Toast.makeText(context, "Settings saved", Toast.LENGTH_SHORT)
        .show();

    }

    catch (Exception e) {

        e.printStackTrace();


    }

从文件中读取的代码是:

        InputStreamReader isr = null;
fileInputStream fIn = null;

    char[] inputBuffer = new char[255];

    String data = null;

    try {

        fIn = openFileInput("gasettings.dat");

        isr = new InputStreamReader(fIn);

        isr.read(inputBuffer);

        data = new String(inputBuffer);



    }

    catch (Exception e) {

        e.printStackTrace();



    }

现在我只能将一个字符串保存到这个文件中。 我喜欢给它写一个 DATE 数组,也想把数据作为数组读回。 我知道 read 方法的返回类型会改变,但我不知道如何读取和写入 DATE 数组或任何其他数组到文件。

谢谢

【问题讨论】:

    标签: android arrays file


    【解决方案1】:

    在这种情况下,您最好的选择是使用 JSON。它将允许您以字符串格式保存数组,将其读回并再次将其转换为原始数组。

    看看这个例子:http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/

    【讨论】:

    • 像小鹿斑比的妈妈一样死了。只需谷歌:“android json 文件”,您将获得比您多年来所能阅读的更多信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多