为什么我的方法不附加 csv 文件?
Why is my method don't append the csv-file?
我尝试将 GPS 数据从我的 Android 7 智能手机导出到 .csv 文件一段时间,只要 isSaveCSV() 为真。
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;
}
}
文件总是一行..我没见过双行文件..
如果你想追加到现有文件,你应该使用带布尔参数的重载构造函数:
FileOutputStream out = new FileOutputStream(csvfile, true);
// Here ---------------------------------------------^
我尝试将 GPS 数据从我的 Android 7 智能手机导出到 .csv 文件一段时间,只要 isSaveCSV() 为真。
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;
}
}
文件总是一行..我没见过双行文件..
如果你想追加到现有文件,你应该使用带布尔参数的重载构造函数:
FileOutputStream out = new FileOutputStream(csvfile, true);
// Here ---------------------------------------------^