【问题标题】:Why is my method don't append the csv-file?为什么我的方法不附加 csv 文件?
【发布时间】:2019-05-18 17:13:23
【问题描述】:

只要 isSaveCSV() 为真,我就会尝试将我的 Android 7 智能手机中的 GPS 数据导出到 .csv 文件一段时间。

public void saveCSVfile() {
    if (isSaveCSV()) {
        Long time = System.currentTimeMillis()/1000;
        String entry =time.toString()+","+lat+","+lon+"\n";
        if (csv_created==false) {
            csv_created=true;
            FILECOUNT++;
            NEWFILENAME = FILENAME+FILECOUNT+".csv";
            File myDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "tensorflow");
            csvfile = new File(myDir, NEWFILENAME);
            try {
                FileOutputStream out = new FileOutputStream(csvfile);
                out.write( entry.getBytes() );
                out.close();
            } catch (final Exception e) {
                LOGGER.e(e, "Exception!");
            }
        }else {

            try {
                entry="TESTFORFOOSAKE";
               FileOutputStream out = openFileOutput(csvfile.getName(), Context.MODE_APPEND);
                out.write(entry.getBytes());
                out.close();
            } catch (final Exception e) {
                LOGGER.e(e, "Exception!");
            }
        }
    }else {
        csv_created=false;
    }
}

文件总是一行..我从未见过双行文件..

【问题讨论】:

    标签: java android android-7.0-nougat


    【解决方案1】:

    如果要追加到现有文件,则应使用带有布尔参数的重载构造函数:

    FileOutputStream out = new FileOutputStream(csvfile, true);
    // Here ---------------------------------------------^
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 2013-05-08
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多