ar-g ar-g - 1 month ago 10
Java Question

When I write again in file with openCsv, this file is overwritten

When I try to write data to a file with opencsv, the file is overwritten instead. All previous data is erased and retains the last data. I used this tutorial http://opencsv.sourceforge.net/#can-I-write , but it didn't help.

case 3:
CSVReader reader = new CSVReader(new FileReader(file));
if (reader.readNext()==null) {
reader.close();
CSVWriter writer = new CSVWriter(new FileWriter(file));
String[]entries="first#second#third".split("#");
writer.writeNext(entries);
writer.close();
}
CSVWriter writer = new CSVWriter(new FileWriter(file),',');
String[]entries = new String[7];
System.out.print("Введите Фамилию(обязательно): ");
entries[0]=check(br.readLine());
System.out.print("Введите имя(обязательно): ");
entries[1]=check(br.readLine());
System.out.print("Введите Отчество(обязательно): ");
entries[2]=check(br.readLine());
System.out.print("Введите телефон мобильный(обязательно): ");
entries[3]=check(br.readLine());
System.out.print("Введите телефон домашний: ");
entries[4]=br.readLine();
System.out.print("Введите адресс: ");
entries[5]=br.readLine();
System.out.print("Введите e-mail: ");
entries[6]=br.readLine();
writer.writeNext(entries);
writer.close();
break;


What should I do to fix this?

fge fge
Answer

If you read the javadoc for FileWriter you will get the answer.

FileWriter has a constructor which allows to append to a file: FileWriter(File file, boolean append)

Comments