Question

I have a program in which I read a CSV file and export a text file type. CSV attributes are name, date of birth, email and address.

I need a BufferedReader through a user can insert the email of one of the records and delete the entire row in the CSV (deleting name, date of birth, and email address) and re-export the text file type.

I do not have much knowledge of Java so much that could help me guide me to the solution.

I share the code,

Help is appreciated,

Thank you!

public class Personas {
private String nombre;
private String fechaNacimiento;
private String email;
private String direccion;

public Personas(String nombre, String fechaNacimiento, String email,
        String direccion) {
    super();
    this.nombre = nombre;
    this.fechaNacimiento = fechaNacimiento;
    this.email = email;
    this.direccion = direccion;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public String getFechaNacimiento() {
    return fechaNacimiento;
}

public void setFechaNacimiento(String fechaNacimiento) {
    this.fechaNacimiento = fechaNacimiento;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getDireccion() {
    return direccion;
}

public void setDireccion(String direccion) {
    this.direccion = direccion;
}


}

Here is the main class

import java.io.*;

public class Principal {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Personas[] miLista = new Personas[100];
    int i = 0;
    String texto = "";
    FileReader lector;
    try {
        lector = new FileReader("C:\\Users\\CD\\Downloads\\dummydata.csv");
        BufferedReader contenido=new BufferedReader(lector);
        try {
            while((texto=contenido.readLine())!=null){
                String[] valores = texto.split(",");
                Personas persona = new Personas(valores[0], valores[1], valores[2], valores[3]);
                    miLista[i]=persona;
                    i++;

            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println("hay mas de 100 registros en el archivo");                       
            System.out.println("solo se cargaran los primeros 100");                        
        }catch(Exception e){
            System.out.println("error desconocido contacte con el desarrollador...");   
            System.out.println(e.getMessage()); 
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
        System.out.print("El archivo no se encuentra...");
    }catch(Exception e){
        System.out.println("error desconocido contacte con el desarrollador...");   
        System.out.println(e.getMessage()); 

    }

    for (Personas persona : miLista) {
        System.out.print(persona.getNombre());
        System.out.print(persona.getDireccion());
        System.out.print(persona.getFechaNacimiento());
        System.out.println(persona.getEmail());
    }

    File miArchivo = new File("miNuevoArchivo.txt");
    try{
        FileWriter fw = new FileWriter(miArchivo);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter wr = new PrintWriter(bw);  
            for (Personas persona : miLista) {
                wr.write(persona.getNombre()+"\t");
                wr.append(persona.getFechaNacimiento()+"\t");
                wr.append(persona.getDireccion()+"\t");
                wr.println(persona.getEmail());
            }
        wr.close();
        bw.close();
    }catch(IOException e){
        System.out.println(e.getMessage());
    }
}

}
Was it helpful?

Solution

I hope it help... but not sure it's what you want to do. Try to not mix differents languages, see try-with-resource, use List (ArrayList) instead of Arrays, create simple functions and read some docs about iterator.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Principal {

    private static final String IN = "C:\\Users\\CD\\Downloads\\dummydata.csv";
    private static final String OUT = "C:\\Users\\CD\\Downloads\\result.txt";

    public static List<Personas> readFile(final String file) throws Exception {
        final List<Personas> result = new ArrayList<Personas>();
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String texto = "";
            while ((texto = reader.readLine()) != null) {
                final String[] valores = texto.split(",");
                final Personas persona = new Personas(valores[0], valores[1], valores[2], valores[3]);
                result.add(persona);
            }
        }
        return result;
    }

    public static void write(final String file, final List<Personas> personas) throws Exception {
        new File(file);
        try (PrintWriter wr = new PrintWriter(new File(file))) {
            for (final Personas persona : personas) {
                wr.write(persona.getNombre() + "\t");
                wr.append(persona.getFechaNacimiento() + "\t");
                wr.append(persona.getDireccion() + "\t");
                wr.println(persona.getEmail());
            }
        }
    }

    public static void main(final String[] args) throws Exception {
        final List<String> emailsToRemove = Arrays.asList("email1@lol.com", "email2@lol.com");

        final List<Personas> personas = readFile(IN);

        //Remove some personnas
        for (Iterator<Personas> it = personas.iterator(); it.hasNext(); /**RIEN**/) {
            Personas act = it.next();
            if(emailsToRemove.contains(act.getEmail())){
                it.remove();
            }
        }

        write(OUT, personas);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top