writefile.java

来自「基于网络编程的例子」· Java 代码 · 共 31 行

JAVA
31
字号
// File: WriteFile.java
//
// Example of saving Java objects to a compressed data file
//

import java.io.*;
import java.util.zip.*;

public class WriteFile {

    static public void main(String [] args) {
	// Create a few objects to save:
	Car mustang = new Car("Mustang", 15000.0f);
	Car ford = new Car("Thunderbird", 22000.0f);
	// Open a compressed output stream:
	String file_name = "car.data";
	// Always wrap file IO in a try/catch statement:
	try {
	    FileOutputStream f = new FileOutputStream(file_name);
	    GZIPOutputStream gf = new GZIPOutputStream(f);
	    ObjectOutputStream oos = new ObjectOutputStream(gf);
	    oos.writeObject(mustang);
	    oos.writeObject(ford);
	    oos.flush();
	    oos.close();
	} catch (Exception e) {
	    System.out.println("Error: " + e);
	}
    }
}

⌨️ 快捷键说明

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