如何将数据附加到新行中的文本文件中,(\ n不起作用)

时间:2013-06-16 08:35:55

标签: android text append newline store

我使用此代码作为将数据写入文本文件的一部分,但我不确定如何将数据附加到新行。 “\ n”似乎不起作用。这里,变量“data”具有以下格式: t = 1,x = -3.1,y = 19.0,z = -8.6 这部分代码迭代,每次都写入变量“data”;目前的结果如下: t = 1,x = -6.9,y = -9.6,z = -6.9t = 2,x = -1.4,y = 6.2,z = 7.0t = 3,x = -1.4,y = 6.1,z = 6.9 t = 4,依此类推,但我想要的是:

t = 1,x = -6.9,y = -9.6,z = -6.9

t = 2,x = -1.4,y = 6.2,z = 7.0

t = 3,x = -1.4,y = 6.1,z = 6.9

提前感谢您的帮助。

        String datatest = data.toString();
        File sdCard = Environment.getExternalStorageDirectory();
        File directory = new File (sdCard.getAbsolutePath() + "/test");
        directory.mkdirs();

        String filename = "test.txt";
        File file = new File(directory, filename);
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(file, true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        try {
            osw.write(datatest + "\n");
            osw.flush();
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

1 个答案:

答案 0 :(得分:3)

尝试“\ r \ n”

并使这个答案更长:)