我正在将一些csv保存到本地android存储上的文件中。我想将新行添加到现有的csv文件中,但我的代码会覆盖以前的数据,我只能看到一行csv文本。请指导我在哪里我错了。这是我现在的代码。
private void saveLogFile(Long activityTimestamp, Long currentTime,
Integer activityType) {
if (activityTimestamp != null && currentTime != null
&& activityType != null) {
File dir = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/ACTIVITY_RECOGNITION");
dir.mkdirs();
File myFile = new File(dir, "activity_log.csv");
try {
if (!myFile.exists()) {
myFile.createNewFile();
}
if (myFile.exists()) {
CSVWriter writer = new CSVWriter(new FileWriter(myFile));
//List<String[]> data = new ArrayList<String[]>();
//data.add(new String[] { activityTimestamp.toString(),currentTime.toString(), activityType.toString() });
String[] data={ activityTimestamp.toString(),currentTime.toString(), activityType.toString() };
writer.writeNext(data);
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Log.d("saveLogFile", "some data is null");
}
}