Java 二维数组存储到 IO 中需要以下步骤:1. 使用 ObjectOutputStream 序列化数组;2. 使用 ObjectInputStream 反序列化数组;3. 使用 PrintStream 将数组写入文本文件(空格分隔);4. 使用 BufferedWriter 将数组写入文本文件(逗号分隔)。

如何将 Java 二维数组存储到 IO 中
将 Java 二维数组存储到输入/输出 (IO) 中的过程涉及以下步骤:
1. 序列化数组
使用 ObjectOutputStream 类将二维数组序列化为字节流。
立即学习“Java免费学习笔记(深入)”;
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("array.dat"));oos.writeObject(array);oos.close();
2. 反序列化数组
阿里云-虚拟数字人
阿里云-虚拟数字人是什么? …
2 查看详情
使用 ObjectInputStream 类将序列化的字节流反序列化为二维数组。
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("array.dat"));int[][] array = (int[][]) ois.readObject();ois.close();
3. 使用 PrintStream 写入数组到文件中
使用 PrintStream 类将二维数组写入文本文件中,每个元素以空格分隔。
try (PrintStream ps = new PrintStream("array.txt")) { for (int[] row : array) { for (int element : row) { ps.print(element + " "); } ps.println(); }} catch (IOException e) { e.printStackTrace();}
4. 使用 BufferedWriter 写入数组到文件中
使用 BufferedWriter 类将二维数组写入文本文件中,每个元素以逗号分隔。
try (BufferedWriter bw = new BufferedWriter(new FileWriter("array.csv"))) { for (int[] row : array) { for (int element : row) { bw.write(element + ","); } bw.newLine(); }} catch (IOException e) { e.printStackTrace();}
以上就是java二维数组怎么存入io中的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/629949.html
微信扫一扫
支付宝扫一扫