Ir al contenido principal

Open CSV

http://opencsv.sourceforge.net/ trata de un framework muy simple y sin mas dependencias de acople que el mismo SDK, a continuación muestro como encapsular mediante Spring, la librería y a su vez leer un archivo csv.


import java.io.Closeable;
import java.io.IOException;

/**
* Common interface to read CSV file extension.
* @author jsanca
*
*/
public interface CsvReaderGenericDAO extends Closeable {

/**
* Return the next row in the CSV file,
* null if not has next.
* @return String []
*/
String [] next () throws IOException;
} // E:O:F:CsvReaderGenericDAO.

Esta clase será nuestra interface común para el lector de Csv. Como puedes ver, simplemente lee la siguiente fila y retorna un vector de cadenas, null en caso que ya no existan mas filas.

import java.io.IOException;

import au.com.bytecode.opencsv.CSVReader;

/**
* http://opencsv.sourceforge.net/ implementation wrapper.
* @author jsanca
*
*/
public class OpenCsvReaderGenericDAOImpl implements CsvReaderGenericDAO {

private CSVReader csvReader = null;

/**
* Constructor.
*/
public OpenCsvReaderGenericDAOImpl(CSVReader csvReader) {

this.csvReader = csvReader;
} // OpenCsvReaderGenericDAOImpl.

/* (non-Javadoc)
* @see cr.smartframework.geo.locator.dao.CsvReaderGenericDAO#next()
*/
@Override
public String[] next() throws IOException {

return this.csvReader.readNext();
} // next.

/* (non-Javadoc)
* @see java.io.Closeable#close()
*/
@Override
public void close() throws IOException {

if (null != this.csvReader) {

this.csvReader.close();
}
} // close.

} // E:O:F:OpenCsvReaderGenericDAOImpl.

Esta sería nuestra implementación, por defecto, implementa la interface antes dicha, junto con la interface closeable.
La implementación simplemente envuelve (wrappea) un CSVReader e invoca al método readNext, para ir obteniendo la siguiente fila.

import java.io.File;
import java.io.FileReader;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

import au.com.bytecode.opencsv.CSVReader;

/**
* Factory to build the OpenCsvReader...
* Lee y parsea a objetos un archivo csv.
* @author jsanca
*
*/
public class OpenCsvReaderGenericDAOFactoryBean implements FactoryBean,
InitializingBean {

private OpenCsvReaderGenericDAOImpl object = null;

private String pathFile = null;

/**
* Obtiene el path del archivo csv.
* @return String
*/
public String getPathFile() {
return pathFile;
} // getPathFile.

/**
* Obtiene el path del archivo csv.
* @param pathFile String
*/
public void setPathFile(String pathFile) {
this.pathFile = pathFile;
} // setPathFile.

/**
* Constructor.
*/
public OpenCsvReaderGenericDAOFactoryBean() {

super ();
} // OpenCsvReaderGenericDAOFactoryBean.

/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
@Override
public Object getObject() throws Exception {

return this.object;
} // getObject.

/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#getObjectType()
*/
@SuppressWarnings("unchecked")
@Override
public Class getObjectType() {

return OpenCsvReaderGenericDAOImpl.class;
} // getObjectType.

/* (non-Javadoc)
* @see org.springframework.beans.factory.FactoryBean#isSingleton()
*/
@Override
public boolean isSingleton() {

return false;
} // isSingleton.

/* (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {

CSVReader csvReader = null;

csvReader = new CSVReader (new FileReader(new File (this.pathFile)));
this.object =
new OpenCsvReaderGenericDAOImpl (csvReader);
} // afterPropertiesSet.

} // E:O:F:OpenCsvReaderGenericDAOFactoryBean.

Ahora realizamos un acomple intrusivo para crear un factorybean de String, el mismo posse la propiedad "pathFile", donde se asigna (comunmente mediante inyección) la locación del archivo. Si hechas un ojo en "afterPropertiesSet"; este método simplemente crea un CSVReader.


<bean id="cvsReaderDAO" class="cr.smartframework.geo.locator.dao.OpenCsvReaderGenericDAOFactoryBean">
<property name="pathFile" value="${csvFilePath}">
</property>



<bean id="algunBean" class="AlgunBean">
<property name="cvsReaderDAO" ref="cvsReaderDAO">
</property>

Por ultimo mostramos el código necesario para asignar nuestro cvsReaderDAO a algún bean.

Comentarios

Entradas más populares de este blog

Pasos para remover Postgresql 8.3 en MAC OS

Tomado de: http://forums.enterprisedb.com/posts/list/1437.page In Mac OSX: (Assuming Default Locations) Via uninstaller: 1) In the installation directory, there will be a uninstall-postgresql.app file will be there, executing (double clicking) that will uninstall the postgresql installation. Manual Uninstallation: 1) Stop the server sudo /sbin/SystemStarter stop postgresql-8.3 2) Remove menu shortcuts: sudo rm -rf /Applications/PostgreSQL 8.3 3) Remove the ini file sudo rm -rf /etc/postgres-reg.ini 4) Removing Startup Items sudo rm -rf /Library/StartupItems/postgresql-8.3 5) Remove the data and installed files sudo rm -rf /Library/PostgreSQL/8.3 6) Delete the user postgres sudo dscl . delete /users/postgres

Enrique Bunbury - 3 de Octubre 2009 - Heredia CR

Via periodico la nacion, me entero de este conciertazo: http://www.nacion.com/viva/2009/julio/29/viva2042768.html Ah esperar mas detalles y la venta de la entradas, "Para solo si me perdonas ..." EDIT ..... Via http://www.enriquebunbury.com/hellville.aspx (gracias a andres por su post) venta anticipada: Entradas a la venta a partir del 13 de agosto en: www.costaricaticket.com Por ahi estare anunciando, el 13 si las estan vendiendo!

Depeche Mode - Costa Rica - 8 de Octubre 2009

Que año de conciertos, primero Jaguares, ahora los Cadillacs y en Octubre Depeche Mode, solo nos falta RadioHead, para ser feliz. Así como lo oyen; desde el sitio principal de Depeche (clickea aquí ), pueden ver la siguiente fecha: Thursday, October 8th, 2009 San Jose, Costa Rica En venta: June 15th, 2009 Demasiado bueno!!!!! La cita será en Octubre, eso si no nos mata la fiebre porcina :( saludos, J