哪位大侠帮我看看我这个程序哪里出错啊 文件读不出来
Customer 类:
package oop_model2;public class Customer  implements java.io.Serializable{
/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * 
 */

private String name;
private String grade;
public Customer(String name, String grade) {

this.name = name;
this.grade = grade;
}
public String toString(){
return "************************************\n"+
       "姓名:"+ this.name+"\n"+
       "会员等级:"+this.grade+"\n";
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MaintainTime 类:
package oop_model2;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;public class MaintainTime  {
/**
 * 
 */

private String type1 = "M2000";
    private String type2 = "DV6400";
private String grade1 = "白金会员";
private String grade2 = "钻石会员";
private Date buytime = new Date(); public  String getMaintainTime(Product prd,Customer cust) {
String mtdate = null;
int mty = 0;
if (prd.getType().equals("M2000")&&cust.getGrade().equals("白金会员")) {
mty = 2;
} else if (prd.getType().equals("M2000")&&cust.getGrade().equals("钻石会员")) {
mty = 5;
}else if (prd.getType().equals("DV6400")&&cust.getGrade().equals("白金会员")) {
mty = 2;
}else if (prd.getType().equals("DV6400")&&cust.getGrade().equals("钻石会员")) {
mty = 5;
}else{
mty=2;
}
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(this.buytime);
int year = gc.get(Calendar.YEAR) + mty;
int month = gc.get(Calendar.MONTH)+1;
int day = gc.get(Calendar.DAY_OF_MONTH)-1;

GregorianCalendar gca = new GregorianCalendar(year, month-1, day);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
mtdate=sdf.format(gca.getTime()); return mtdate;
}
}
OrderInfo 类:
package oop_model2;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
public class OrderInfo implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * 
 */ private Customer cust;
private Product prod;
private String mtdate;
public OrderInfo(Customer cust, Product prod, String mtdate) {

this.cust = cust;
this.prod = prod;
this.mtdate = mtdate;
}
public OrderInfo() {
// TODO Auto-generated constructor stub
}
public OrderInfo inputInfo() throws IOException{
OrderInfo order=null;
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
System.out.println("客户姓名:");
String name=reader.readLine();
System.out.println("会员等级:");
String grade=reader.readLine();
Customer cust=new Customer(name,grade);

System.out.println("产品编号:");
String pno=reader.readLine();
System.out.println("产品型号:");
String type=reader.readLine();
Product prod=new Product(pno,type);


MaintainTime mt=new MaintainTime();
String mtdate=mt.getMaintainTime(prod,cust);
order=new OrderInfo(cust,prod,mtdate);

return order;
}
public String toString(){
return this.cust.toString()+this.prod.toString()+"该产品保修期截止到:"+this.mtdate;
}}
Product 类:
package oop_model2;public class Product  implements java.io.Serializable{
/**
 * 
 */
private static final long serialVersionUID = 1L;
/**
 * 
 */

private String pno;
private String type;
public Product(String pno, String type) {

this.pno = pno;
this.type = type;
}
public String toString(){
return "产品编号:"+this.pno+"\n"+
       "产品型号:"+this.type+"\n";
}
public String getPno() {
return pno;
}
public void setPno(String pno) {
this.pno = pno;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
ReadAndWrite 类:
package oop_model2;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;public class ReadAndWrite {
String dir = "c:\\java035"; String filename = "order.bin"; File mydir = new File(dir); File myfile = new File(mydir, filename); public boolean isMyDirExists() {
boolean flag = false;
if (!mydir.exists()) { flag = false;
} else {
flag = true;
} return flag;
} public boolean creatMyDir() {
boolean flag = false;
if (isMyDirExists()) {
flag = true;
System.out.println("目录已经存在,可以继续操作..."); } else {
if (!mydir.mkdirs()) {
flag = false;
System.out.println("目录创建失败...");
} else {
flag = true;
System.out.println("目录创建成功...");
}
} return flag;
} public void writeInfoToFile() throws IOException {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
boolean flag = creatMyDir();
if (flag) {
fos = new FileOutputStream(myfile, true);
} else {
System.exit(1);
}
boolean check = myfile.exists() && myfile.length() > 0;
if (check) {
oos = new ObjectOutputStream(fos);
oos.reset();
System.out.println("利用流重置的方式,建立对象输出流。对象输出流建立成功...");
} else {
oos = new ObjectOutputStream(fos);
System.out.println("对象输出流建立成功...");
}
// 建立要写入的对象
OrderInfo oi = new OrderInfo();
OrderInfo order = oi.inputInfo();
System.out.println(order.toString());
oos.writeObject(order);
if (oos != null) {
oos.close();
System.out.println("对象输出流关闭...");
} }
public void readInfoFromFile() throws IOException{
FileInputStream fis=null;
ObjectInputStream ois=null;
if(myfile.exists()){
System.out.println("文件存在,可以继续操作");
fis=new FileInputStream(myfile);
ois=new ObjectInputStream(fis);
try {

OrderInfo order=(OrderInfo) ois.readObject();
System.out.println(order.toString());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("文件不存在,无法读取!!!");
}
if(ois!=null){
ois.close();
System.out.println("对象输入流关闭");
}
}
}
Testing 类:
package oop_model2;import java.io.IOException;public class Testing {
public static void main(String args[]) throws IOException{
ReadAndWrite rw=new ReadAndWrite();
rw.readInfoFromFile();

}}
总共上面6个类,运行时老读取不了写进文件的信息,报下面的错误:
Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at oop_model2.ReadAndWrite.readInfoFromFile(ReadAndWrite.java:89)
at oop_model2.Testing.main(Testing.java:8)
文件存在,可以继续操作
搞了半天不知道哪里出错了,就是读不出来

解决方案 »

  1.   

    交流群 C++ java 36414176
      

  2.   

    问:
    报下面的错误: 
    Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC 
    at java.io.ObjectInputStream.readObject0(Unknown Source) 
    at java.io.ObjectInputStream.readObject(Unknown Source) 
    at oop_model2.ReadAndWrite.readInfoFromFile(ReadAndWrite.java:89) 
    at oop_model2.Testing.main(Testing.java:8) 
    文件存在,可以继续操作 
    搞了半天不知道哪里出错了,就是读不出来 答:是对象序列化存盘的文件已受损造成的。你重新生成一下序列化的文件,然后再读就一切正常了。