writecountryinfo.java
来自「java源程序 对初学者有很大的帮助 从简单到复杂」· Java 代码 · 共 44 行
JAVA
44 行
//********************************************************************
// WriteCountryInfo.java Author: Lewis/Loftus
//
// Demonstrates object serialization.
//********************************************************************
import java.io.*;
public class WriteCountryInfo
{
//-----------------------------------------------------------------
// Creates several objects, prints them to standard output, and
/// serializes them to a file.
//-----------------------------------------------------------------
public static void main (String[] args) throws IOException
{
FileOutputStream file = new FileOutputStream ("countries.dat");
ObjectOutputStream outStream = new ObjectOutputStream (file);
CountryInfo[] countries = new CountryInfo[5];
countries[0] = new CountryInfo ("United States of America",
"USA", "Washington, D.C.", 9629091L, 278058900L);
countries[1] = new CountryInfo ("Russia", "RUS", "Moscow",
17075200L, 145470200L);
countries[2] = new CountryInfo ("Italy", "ITA", "Rome",
301230L, 57679800L);
countries[3] = new CountryInfo ("Sweden", "SWE", "Stockholm",
449964L, 8875100L);
countries[4] = new CountryInfo ("Poland", "POL", "Warsaw",
312685L, 38633900L);
int scan;
// Print the objects
for (scan = 0; scan < countries.length; scan++)
System.out.println (countries[scan]);
// Serialize the objects to a file
for (scan = 0; scan < countries.length; scan++)
outStream.writeObject (countries[scan]);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?