Originariamente inviato da Naruto 92
purtroppo bisogna scorrersi gli elementi ad uno ad uno
Non è proprio così... se sai che cosa stai andando a leggere non importa.
codice:
import java.io.*;
/**
*
* @author Andrea
*/
public class SerializationTest implements Serializable {
private File file;
private Object obj;
private static final String DEFAULT_PATH = "C:/Users/Andrea/Desktop/serialize.dat";
public SerializationTest (Object obj) {
this(DEFAULT_PATH, obj);
}
public SerializationTest (String path, Object obj) {
this.file = new File(path);
this.obj = obj;
}
public void save() {
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
oos.flush();
oos.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public Object read() {
Object obj = null;
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
obj = ois.readObject();
ois.close();
}
catch (Exception e) {
e.printStackTrace();
}
return obj;
}
public static void main (String[] args) {
int[][] a = new int[][]
{
{1, 2, 3},
{4, 5, 6},
};
SerializationTest test = new SerializationTest(a);
test.save();
System.out.println("Salvato");
int[][] b = (int[][])test.read();
System.out.println("Letto. Proviamo a modificare");
b[1][1] *= 5;
//Stampiamo
for (int i = 0; i < b.length; i++) {
System.out.print("[ ");
for (int j = 0; j < b[0].length; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println("]");
}
}
}
il che in output dovrebbe darti
codice:
Salvato
Letto. Proviamo a modificare
[ 1 2 3 ]
[ 4 25 6 ]
L'assunto è però che tu sappia che cosa tu stia leggendo, altrimenti il cast fallisce miseramente.