【问题标题】:Android .write to .csv-file, file gets created but no text is savedAndroid .write 到 .csv 文件,文件被创建但没有文本被保存
【发布时间】:2016-02-15 22:06:26
【问题描述】:

我有一个通过蓝牙从传感器收集数据的安卓应用程序。

当尝试将数据保存到设备上的 .csv 文件时,会创建 data.csv 文件,但文件中不会保存任何文本。

有问题的函数:

private void writeData(boolean writeError) {
    try {
        File traceFile = new File(Environment.getExternalStorageDirectory(), writeError ? "error.csv" : "data.csv");
        if (!traceFile.exists())
            traceFile.createNewFile();
        BufferedWriter writer = new BufferedWriter(new FileWriter(traceFile, true /*append*/));

        writer.write("Test string");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

不会引发错误,并且我已确保代码的每个部分都已执行。关于为什么这不起作用的任何想法?

【问题讨论】:

  • 你需要flush()close()作者。
  • 成功了,谢谢!

标签: android csv bluetooth writer


【解决方案1】:

Hurundi V. Bakshi的解决方案

添加

writer.flush();

之后

writer.write("Test string");

关于 flush() 的文档:

Flushes this writer. Implementations of this method should ensure that all buffered characters are written to the target.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多