Buenas tarde foreros, tengo un ligero problema al guardar un ArrayList en un fichero :/
Alguien ve el error? puede ser tema de permisos? (trabajo bajo Linux)
private static void exportaProductos(ArrayList p){
ObjectOutputStream fichero = null;
try {
fichero = new ObjectOutputStream(new FileOutputStream("productos.mio"));
for(int x = 0; x<p.size(); x++){
fichero.writeObject(p.get(x));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
fichero.close();
}catch(IOException f){}
}
}
Se me olvidó " implements Serializable " ;D ;D
Lol xD. Suele pasar :xD
PD: Puedes aprovechar el autocloseable:
public void exportProducts(List<ProductVO> products) {
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:\\products.obj"))) {
for(ProductVO product : products)
out.writeObject(product);
} catch(FileNotFoundException ex) {
ex.printStackTrace();
} catch(IOException ex2) {
ex2.printStackTrace();
}
}
Así ya no tienes que preocuparte por cerrar flujos o conexiones ;)
Cita de: Gus Garsaky en 2 Marzo 2015, 20:44 PM
Lol xD. Suele pasar :xD
PD: Puedes aprovechar el autocloseable:
public void exportProducts(List<ProductVO> products) {
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:\\products.obj"))) {
for(ProductVO product : products)
out.writeObject(product);
} catch(FileNotFoundException ex) {
ex.printStackTrace();
} catch(IOException ex2) {
ex2.printStackTrace();
}
}
Así ya no tienes que preocuparte por cerrar flujos o conexiones ;)
Muchas gracias no sabia que podía hacerlo de esa manera, me voy a ahorrar unas cuentas lineas de codigo ;)