@@ -31,47 +31,42 @@ public Punto(int coordX, int coordY) {
31
31
public Punto () {
32
32
}
33
33
34
- public static void almacenarPuntosEnArchivo (File f1 , File f2 ) throws IOException {
34
+ public static void almacenarPuntosEnArchivo (File f1 , File f2 ){
35
35
Punto p ;
36
- ArrayList <Punto > lista = new ArrayList <>();
37
36
try {
37
+ if (!f2 .exists ()){
38
+ ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (f2 ));
39
+ oos .close ();
40
+ }
38
41
DataInputStream dis = new DataInputStream (new FileInputStream (f1 ));
42
+ MiClaseOutput co = new MiClaseOutput (new FileOutputStream (f2 , true ));
39
43
try {
40
44
while (true ) {
41
45
p = new Punto (dis .readInt (), dis .readInt ());
42
- lista . add (p );
46
+ co . writeObject (p );
43
47
}
44
48
} catch (EOFException e ) {
45
49
dis .close ();
50
+ co .close ();
46
51
System .err .println ("End of file" );
47
52
}
48
53
} catch (FileNotFoundException ex ) {
49
54
System .err .println ("File not found" );
55
+ } catch (IOException ex ) {
56
+ Logger .getLogger (Punto .class .getName ()).log (Level .SEVERE , null , ex );
50
57
}
51
- if (f2 .exists ()) {
52
- MiClaseOutput co = new MiClaseOutput (new FileOutputStream (f2 , true ));
53
- for (Punto punto : lista ) {
54
- co .writeObject (punto );
55
- }
56
- co .close ();
57
- }else {
58
- ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (f2 ));
59
-
60
- for (Punto punto : lista ) {
61
- oos .writeObject (punto );
62
- }
63
- oos .close ();
64
58
65
- }
66
59
}
67
60
68
61
public static ArrayList <Punto > obtenerArrayListDeArchivo (File f ){
69
62
ArrayList <Punto > lista = new ArrayList <>();
70
63
try {
71
64
ObjectInputStream ois = new ObjectInputStream (new FileInputStream (f ));
72
65
try {
73
- Punto p = (Punto ) ois .readObject ();
74
- lista .add (p );
66
+ while (true ) {
67
+ Punto p = (Punto ) ois .readObject ();
68
+ lista .add (p );
69
+ }
75
70
} catch (EOFException e ) {
76
71
ois .close ();
77
72
System .err .println ("End of file" );
0 commit comments