Skip to content

Commit 3c48d99

Browse files
committed
Correccion
1 parent 130e004 commit 3c48d99

File tree

5 files changed

+17
-28
lines changed

5 files changed

+17
-28
lines changed

Diff for: files/puntos.obj

211 Bytes
Binary file not shown.
23 Bytes
Binary file not shown.
Binary file not shown.

Diff for: src/ficherosSerializables/punto/Main.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package ficherosSerializables.punto;
22

33
import java.io.File;
4-
import java.io.IOException;
54
import java.util.ArrayList;
6-
import java.util.logging.Level;
7-
import java.util.logging.Logger;
85

96
/**
107
*
@@ -15,12 +12,9 @@ public static void main(String[] args) {
1512
File f1 = new File("files/puntos.bin");
1613
File f2 = new File("files/puntos.obj");
1714
ArrayList<Punto> puntos = new ArrayList<>();
18-
try {
19-
Punto.almacenarPuntosEnArchivo(f1, f2);
20-
puntos = Punto.obtenerArrayListDeArchivo(f2);
21-
} catch (IOException ex) {
22-
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
23-
}
15+
Punto.almacenarPuntosEnArchivo(f1, f2);
16+
puntos = Punto.obtenerArrayListDeArchivo(f2);
17+
2418
for (Punto punto : puntos) {
2519
System.out.println(punto.toString());
2620
}

Diff for: src/ficherosSerializables/punto/Punto.java

+14-19
Original file line numberDiff line numberDiff line change
@@ -31,47 +31,42 @@ public Punto(int coordX, int coordY) {
3131
public Punto() {
3232
}
3333

34-
public static void almacenarPuntosEnArchivo(File f1, File f2) throws IOException{
34+
public static void almacenarPuntosEnArchivo(File f1, File f2){
3535
Punto p;
36-
ArrayList<Punto> lista = new ArrayList<>();
3736
try {
37+
if(!f2.exists()){
38+
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f2));
39+
oos.close();
40+
}
3841
DataInputStream dis = new DataInputStream(new FileInputStream(f1));
42+
MiClaseOutput co = new MiClaseOutput(new FileOutputStream(f2, true));
3943
try {
4044
while (true) {
4145
p = new Punto(dis.readInt(), dis.readInt());
42-
lista.add(p);
46+
co.writeObject(p);
4347
}
4448
} catch (EOFException e) {
4549
dis.close();
50+
co.close();
4651
System.err.println("End of file");
4752
}
4853
} catch (FileNotFoundException ex) {
4954
System.err.println("File not found");
55+
} catch (IOException ex) {
56+
Logger.getLogger(Punto.class.getName()).log(Level.SEVERE, null, ex);
5057
}
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();
6458

65-
}
6659
}
6760

6861
public static ArrayList<Punto> obtenerArrayListDeArchivo(File f){
6962
ArrayList<Punto> lista = new ArrayList<>();
7063
try {
7164
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
7265
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+
}
7570
} catch (EOFException e) {
7671
ois.close();
7772
System.err.println("End of file");

0 commit comments

Comments
 (0)