二维阵列存储

维迪姆

我最近偶然发现了这个问题。我想将二维int数组存储在文件中以供以后读取。除了简单的txt.file以外,还有其他方法吗?这是我的第一篇文章,请原谅我的英语。(Java作为编程语言)

桑耶夫

如@Andy所示,您可以用于ObjectOutputStream将数组序列化为文件

int[][] intArray = new int[5][5];
//Code to populate array

// serialize array
FileOutputStream fos = new FileOutputStream("array.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(intArray);

然后可以使用文件从数组中读取它作为数组 ObjectInputStream

FileInputStream fis = new FileInputStream("array.dat");
ObjectInputStream iis = new ObjectInputStream(fis);
intArray = (int[][]) iis.readObject();

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章