简体   繁体   English

为什么我的方法不附加csv文件?

[英]Why is my method don't append the csv-file?

I try to export the GPS-Data from my Android 7 smartphone to a .csv file for some time as long as isSaveCSV() is true. 只要isSaveCSV()为true,我会尝试将GPS数据从我的Android 7智能手机导出到.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;
    }
}

The file is always one row.. I never saw a double line file.. 该文件始终是一行。.我从未见过双行文件。

If you want to append to an existing file, you should use the overloaded constructor that takes a boolean argument: 如果要追加到现有文件,则应使用带有布尔参数的重载构造函数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM