Question:
I am trying to modify the data of a CSV file from Java.
In my file CSV
I have the following data saved:
"Nombre","Apellido","Intereses","Descripcion"
jose,guerra,algo,algo
pedro,perez,algo,algo
manuel,aguirre,algo,algo
Persona
class:
public class Persona {
String Nombre;
String Apellido;
String Intereses;
String Descripcion;
public Persona(String Nombre,String Apellido,String Intereses,String Descripcion)
{
this.Nombre = Nombre;
this.Apellido= Apellido;
this.Intereses = Intereses;
this.Descripcion = Descripcion;
}
//respectivos métodos get y set
public void setNome(String nombre){
this.Nombre = nombre;
}
}
and another métodos
class:
import java.util.ArrayList;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import com.opencsv.*;
import java.io.File;
public class métodos {
static ArrayList<Persona> personas = new ArrayList<Persona>();
static String directory = "C:\\Users\\BryanPC\\Desktop\\personas.txt";
public métodos() {
}
// escribe los datos salvados en el ArrayList Personas (hay un error se
// imprimen 4 comillas de mas)
public static void escribeCSV() throws FileNotFoundException, IOException {
ArrayList<String[]> datos = personasToArrayListOfString();
CSVWriter csvOutput = new CSVWriter(new FileWriter(directory, true), ',', '"', "\r\n");
csvOutput.writeAll(datos, false);
csvOutput.close();
}
// lee los datos del CSV y los salva en el ArrayList Personas
public static void leeCSV() throws FileNotFoundException, IOException {
File file = new File(directory);
Scanner input = new Scanner(file);
input.next();
while (input.hasNext()) {
String s = input.next();
String[] data = s.split(",");
Persona p = arrayOfStringsToPersona(data);
personas.add(p);
}
}
// cambia el nombre de la primera persona salvada en el ArrayList Personas
// por Juan
public static void modificaCSV() {
for (Persona p : personas) {
p.setNome("juan");
break;
}
}
// transforma un objeto de tipo Persona a un ArrayList<String[]> para
// poder imprimirlo
public static ArrayList<String[]> personasToArrayListOfString() {
ArrayList<String[]> datos = new ArrayList<String[]>();
for (Persona p : personas) {
String[] s = new String[10];
String nombre = p.getNombre();
String apellido = p.getApellido();
String intereses = p.geIntereses();
String descripcion = p.getDescripcion();
s[0] = nombre;
s[1] = apellido;
s[2] = intereses;
s[3] = descripcion;
datos.add(s);
}
return datos;
}
// transforma un objeto de tipo String[] a uno de tipo Persona para asi
// poder añadirlo al ArrayList Personas
public static Persona arrayOfStringsToPersona(String[] a) {
String nombre = a[0];
String apellido = a[1];
String intereses = a[2];
String descripcion = a[3];
Persona p = new Persona(nombre, apellido, intereses, descripcion);
return p;
}
public static void main(String[] args) throws FileNotFoundException, IOException {
leeCSV();
// borra o sobreescribe el csv con nuevo nombre "juan"
modificaCSV();
metodoFaltante();
escribeCSV();
}
}
I guess once you use the leeCSV method you will have to create another method that clears the data in the CSV file.
Or overwrite everything that is written in the CSV file.
Answer:
If what you want is to add data to the file (attach), what you do is correct:
new FileWriter(directory, true)
What you want is to delete the data and write new:
new FileWriter(directory, false)
Check the documentation: https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html
public FileWriter (String fileName, boolean append)
append true, the data is written to the end of the file.
append false, the data is written to the file.