将数组存储到文件中可以通过文件输出流实现,具体步骤如下:创建输出流FileOutputStream fos = new FileOutputStream("array.txt");创建 ObjectOutputStreamObjectOutputStream oos = new ObjectOutputStream(fos);将数组序列化到文件中oos.writeObject(array);关闭流oos.close();
将数组存储到文件
在 Java 中,我们可以使用文件输出流将数组存储到文件中。具体步骤如下:
1. 创建输出流
FileOutputStream fos = new FileOutputStream("array.txt");2. 创建 ObjectOutputStream
ObjectOutputStream oos = new ObjectOutputStream(fos);
3. 将数组序列化到文件中
oos.writeObject(array);
4. 关闭流
oos.close();
示例代码:
int[] array = {1, 2, 3, 4, 5};
try (FileOutputStream fos = new FileOutputStream("array.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(array);
} catch (IOException e) {
e.printStackTrace();
}注意: