求写个java的小程序。谢谢!
翻译了一下大意:写一个名字叫做Carplate的类封装了汽车牌照的概念,假设它又如下的属性:牌照号、状态和颜色。写一个客户端程序,程序可以创建3个Carplate对象并把...
翻译了一下大意:写一个名字叫做Carplate的类封装了汽车牌照的概念,假设它又如下的属性:牌照号、状态和颜色。写一个客户端程序,程序可以创建3个Carplate对象并把他们写进一个文件做为对象,然后读取他们从文件中做为对象,输出每个对象的描述用toString方法(每一个Carplate类需要被覆盖),并输出对象的号。当读这些对象时,你应该假设不知道文件中对象的号,并且检测文件的末尾当EOFException被抛出通过readObject方法。
肯定有翻译错的地方,麻烦各位大侠了。
刚刚发过一个一样的还发了50分竟然自动没了,这是为什么啊。。。。。。。。。 展开
肯定有翻译错的地方,麻烦各位大侠了。
刚刚发过一个一样的还发了50分竟然自动没了,这是为什么啊。。。。。。。。。 展开
2个回答
展开全部
这个简单,网上也有相关的例子,关于ObjectOutputStream的用法
package objs;
import java.io.Serializable;
public class Carplate implements Serializable {
private static final long serialVersionUID = -6670528088216041285L;
private String number;
private String state;
private String color;
public Carplate(String pNum,String pState,String pColor){
this.number=pNum;
this.state=pState;
this.color=pColor;
}
public String toString(){
return "number:"+this.state+",state:"+state+",color:"+color;
}
}
//===============================================
package objs;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class CarplateTest {
public static void main(String[] args){
Carplate carp1=new Carplate("A123","on","黑色");
Carplate carp2=new Carplate("B888","off","白色");
Carplate carp3=new Carplate("C666","off","蓝色");
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("D:\\tool lib\\data.txt"));
oos.writeObject(carp1);
oos.writeObject(carp2);
oos.writeObject(carp3);
carp1 = null;
carp2 = null;
carp3 = null;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
oos = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 把对象从文件读入.
ObjectInputStream ois = null;
int objCount=0;
try {
ois = new ObjectInputStream(new FileInputStream("D:\\tool lib\\data.txt"));
while(true){
// 输出从文件中读取到的学生的信息, 用于查检是否正确
Carplate aCarp=(Carplate) ois.readObject();
objCount++;
System.out.println(aCarp.toString());
}
}catch(EOFException eofe){
System.out.println("对象读取完成!");
}catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
System.out.println("对象个数:"+objCount);
if (ois != null) {
try {
ois.close();
ois = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package objs;
import java.io.Serializable;
public class Carplate implements Serializable {
private static final long serialVersionUID = -6670528088216041285L;
private String number;
private String state;
private String color;
public Carplate(String pNum,String pState,String pColor){
this.number=pNum;
this.state=pState;
this.color=pColor;
}
public String toString(){
return "number:"+this.state+",state:"+state+",color:"+color;
}
}
//===============================================
package objs;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class CarplateTest {
public static void main(String[] args){
Carplate carp1=new Carplate("A123","on","黑色");
Carplate carp2=new Carplate("B888","off","白色");
Carplate carp3=new Carplate("C666","off","蓝色");
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("D:\\tool lib\\data.txt"));
oos.writeObject(carp1);
oos.writeObject(carp2);
oos.writeObject(carp3);
carp1 = null;
carp2 = null;
carp3 = null;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
oos = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 把对象从文件读入.
ObjectInputStream ois = null;
int objCount=0;
try {
ois = new ObjectInputStream(new FileInputStream("D:\\tool lib\\data.txt"));
while(true){
// 输出从文件中读取到的学生的信息, 用于查检是否正确
Carplate aCarp=(Carplate) ois.readObject();
objCount++;
System.out.println(aCarp.toString());
}
}catch(EOFException eofe){
System.out.println("对象读取完成!");
}catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
System.out.println("对象个数:"+objCount);
if (ois != null) {
try {
ois.close();
ois = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询