Java中的输入输出流是程序与外界进行数据交换的桥梁,所有流对象都是位于java.io包下。流按方向分为输入流和输出流,按性质分为字节流、过滤流、基本数据类型流、对象流。
InputStream/OutputStream 所有输入输入流的父类,是抽象类。在创建对象流对象的时候,尽量用父类的对象名取接受子类的对象。调用方法的时候,尽量调用父类中定义的方法。子类有:FileInputStream/FileOutputStream 读取文件的数据/写数据到文件 ,DataInputStream/DataOutputStream 读写指定的java中的基本数据类型类型,BufferedInputStream/BufferedOutputStream 读取数据时将其存入到JVM内存的缓冲区中,待达到一定的程度再将其读取出来,ObjectInputStream/ObjectOutputStream 对象的读取。
使用ObjectInputStream/ObjectOutputStream 时,要切记将对象序列化。对象序列化是把一个对象通过I/O流写到文件上的过程。序列化接口Serializable
在此接口中没有任何的方法。
以下是通过DataInputStream/DataOutputStream 类来实现画板的保存的代码:
public class FilePreserveOpen {
private EGraphList<Graph> ejl;
public FilePreserveOpen(EGraphList<Graph> ejl) {
this.ejl = ejl;
}
/**
* 保存图形到文件中
*
*/
public void saveFile() {
String fileName = "\\画板\\draw\\src\\draw\\graph";
try {
FileOutputStream os = new FileOutputStream(fileName);
DataOutputStream dos = new DataOutputStream(os);
// 输出图形的总数到文件
dos.writeInt(ejl.size());
// 循环队列中的图形数据
for (int i = 0; i < ejl.size(); i++) {
// 得到Graph对象
Graph graph = ejl.get(i);
// 得到图形的属性
String str = graph.getitem();
// 输出图形的属性到文件中
dos.writeUTF(str);
if (str != "eraser") {
// 得到图形的颜色
Color col = graph.getColor();
// 得到颜色的红、绿、蓝的分量
int red = col.getRed();
int green = col.getGreen();
int blue = col.getBlue();
// 输出图形的颜色到文件中
dos.writeInt(red);
dos.writeInt(green);
dos.writeInt(blue);
} else if (str == "eraser") {
// 得到图形的颜色
Color col = java.awt.Color.WHITE;
// 得到颜色的红、绿、蓝的分量
int red = col.getRed();
int green = col.getGreen();
int blue = col.getBlue();
// 输出图形的颜色到文件中
dos.writeInt(red);
dos.writeInt(green);
dos.writeInt(blue);
}
if (!str.equals("polygon")) {
// 得到图形的坐标
int x1 = graph.getX1();
int y1 = graph.getY1();
int x2 = graph.getX2();
int y2 = graph.getY2();
// 输出图形的坐标到文件中
dos.writeInt(x1);
dos.writeInt(y1);
dos.writeInt(x2);
dos.writeInt(y2);
} else if (str.equals("polygon")) {
int[] xpoint = graph.getXPoint();
int[] ypoint = graph.getYPoint();
int npoint = graph.getNPoint();
dos.writeInt(npoint);
for (int k = 0; k < xpoint.length; k++) {
dos.writeInt(xpoint[k]);
dos.writeInt(ypoint[k]);
}
}
}
// 强制写入并关闭
dos.flush();
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 打开文件,并获取存储在文件中的数据
*
* @return 返回所有的数据
*/
@SuppressWarnings("null")
public EGraphList<Graph> openFile() {
// 实例化一个自定义队列对象
EGraphList<Graph> graphlist = new EGraphList<Graph>();
String fileName = "\\画板\\draw\\src\\draw\\graph";
try {
FileInputStream fis = new FileInputStream(fileName);
DataInputStream dis = new DataInputStream(fis);
// 输入图形的总数到文件
int size = dis.readInt();
// 循环队列中的图形数据
for (int i = 0; i < size; i++) {
String str = dis.readUTF();
// 得到颜色分量、图形属性、坐标值
int red = dis.readInt();
int green = dis.readInt();
int blue = dis.readInt();
Color col = new Color(red, green, blue);
if (!str.equals("polygon")) {
int x1 = dis.readInt();
int y1 = dis.readInt();
int x2 = dis.readInt();
int y2 = dis.readInt();
// 实例化Graph对象
Graph graph = new Graph(str, col, x1, y1, x2, y2);
graphlist.add(graph);
} else if (str.equals("polygon")) {
int npoint = dis.readInt();
int[] xpoint = new int[npoint], ypoint = new int[npoint];
//写入多边形时,先写入多边形的总数
for (int k = 0; k < npoint; k++) {
xpoint[k] = dis.readInt();
ypoint[k] = dis.readInt();
}
// 实例化Graph对象
Graph graph = new Graph(str, col, xpoint, ypoint, npoint);
graphlist.add(graph);
}
}
// 关闭
dis.close();
} catch (Exception e) {
e.printStackTrace();
}
return graphlist;
}
}