İçerdikleri nesneler olmayabilir, bu nedenle dizinin içeriğinin serileştirilebildiğini kontrol edin - eğer yapmadıysa yapın.
İşte bir örnek, ints dizilerini kullanarak.
public static void main(String[] args) {
int[][] twoD = new int[][] { new int[] { 1, 2 },
new int[] { 3, 4 } };
int[][] newTwoD = null; // will deserialize to this
System.out.println("Before serialization");
for (int[] arr : twoD) {
for (int val : arr) {
System.out.println(val);
}
}
try {
FileOutputStream fos = new FileOutputStream("test.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(twoD);
FileInputStream fis = new FileInputStream("test.dat");
ObjectInputStream iis = new ObjectInputStream(fis);
newTwoD = (int[][]) iis.readObject();
} catch (Exception e) {
}
System.out.println("After serialization");
for (int[] arr : newTwoD) {
for (int val : arr) {
System.out.println(val);
}
}
}
Çıktı:
Before serialization
1
2
3
4
After serialization
1
2
3
4
Hm, tamam. Çok boyutlu dizim tamsayılardan başka bir şey barındırmıyor, ancak dizinin kendisinin gerçekten serileştirilebileceğini bildiğim için teşekkürler ... – littleK
Benim için ints ile çalışır - neden bir sorun gördüğünüzden emin değilim. İyi şanslar! – Brabster