⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialization.java

📁 国外的数据结构与算法分析用书
💻 JAVA
字号:
import java.io.*;

/**	A small program that creates a small array, serializes it, and writes it to
	a file.  The array object is then discarded and the array  read back in from the file. */
public class Serialization
{
	public static void main(String[] args) throws Exception
	{
		int[] array = {3, 45, 53, 13, -5};
		for (int i = 0; i < array.length; i++)
			System.out.print(array[i] + " "); // display array contents
		System.out.println();
		FileOutputStream fos = new FileOutputStream("SerializedData");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(array); // write serialized array to file
		oos.flush();
		oos.close();

		array = null; // discard the array object
		System.out.println("array is now: " + array); // show that array is gone

		FileInputStream fis = new FileInputStream("SerializedData");
		ObjectInputStream ois = new ObjectInputStream(fis);
		array = (int[]) ois.readObject(); // read array back from file
		ois.close();
		for (int i = 0; i < array.length; i++)
			System.out.print(array[i] + " "); //display array contents
		System.out.println();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -