2011-11-24 13 views
9

El siguiente código es el que tengo actualmente, sin embargo sobrescribe cualquier dato en el archivo csv en ese momento, en lugar de anexarlo al final. ¿Hay una forma fácil de hacer esto?Anexando a la última línea del archivo CSV en Java

public void printCustomerList() throws IOException{ 
     FileWriter pw = new FileWriter("F:\\data.csv"); 
     Iterator s = customerIterator(); 
     if (s.hasNext()==false){ 
      System.out.println("Empty"); 
     } 
     while(s.hasNext()){ 
      Customer current = (Customer) s.next(); 
      System.out.println(current.toString()+"\n"); 
      pw.append(current.getName()); 
      pw.append(","); 
      pw.append(current.getAddress()); 
      pw.append("\n"); 
     } 
      pw.flush(); 
      pw.close(); 
    } 
+1

Posible duplicado de http://stackoverflow.com/questions/6027764/how-to-append-data-to-a-file. –

Respuesta

24

Intenta abrir el archivo como esto

FileWriter pw = new FileWriter("F:\\data.csv",true); 

Pass true argumento para anexar.

+0

gracias. Eso ayudo. –

3

uso FileWriter pw = new FileWriter("F:\\data.csv", true);

referencia: FileWriter

3

Es necesario utilizar un constructor diferente para su FileWriter:

FileWriter pw = new FileWriter("F:\\data.csv", true); 

Para más información ver la JDK API for this constructor.

Cuestiones relacionadas