2

我使用此代码作为将数据写入文本文件的一部分,但我不确定如何将数据附加到新行。"\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.9t=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();
        }
4

1 回答 1

3

试试“\r\n”

并使这个答案更长:)

于 2013-06-16T11:22:17.303 回答