您的位置:首页 > 移动开发 > Objective-C

11. 23. 4. 读写一个多边形 Reading Basic Data From an Object Stream

2011-10-10 19:38 453 查看
import java.awt.Polygon;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ReadBasicData {

public static void main(String[] args) {
int[] x = new int[]{1,2,3};
int[] y = new int[]{4,5,6};

/*
* 参数:
* xpoints - X 坐标的数组
* ypoints - Y 坐标的数组
* npoints - 此 Polygon 中点的总数
*/
Polygon polygon = new Polygon(x, y, x.length);//一个多边形

try{//从文件写入流套缓冲写入流再套上Object写入流
ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(
new FileOutputStream("Polygon.bin")));//用一个二进制格式 .bin 来存储
objectOut.writeObject(polygon);//写入一个多边形
objectOut.close();
}catch(IOException e){
e.printStackTrace();
}

try{
ObjectInputStream objectIn = new ObjectInputStream(new BufferedInputStream(
new FileInputStream("Polygon.bin")));
Polygon theLine = (Polygon)objectIn.readObject();//读出来再转成Polygon
System.out.println(theLine);
objectIn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
/*
* java.awt.Polygon@1270b73
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: