Java ObjectInputStream。只能写一个对象,不能一直写入是为什么?读出对象也不对。
publicstaticCustomer[]rrBank()throwsEOFException{Customer[]cus=newCustomer[100];Custo...
public static Customer[] rrBank() throws EOFException{
Customer[] cus=new Customer[100];
Customer c;
try {
ObjectInputStream is = new ObjectInputStream(new FileInputStream(
"wxps.txt"));
bankInstance=(Bank)is.readObject();
for(int i=0;i<getBank().getNumOfCustomers();i++){
cus[i]=(Customer) is.readObject();
}
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return cus;
}
public static void ssBank(){
try {
ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream("wxps.txt"));
os.writeObject(bankInstance);
for(int i=0;i<getBank().getNumOfCustomers();i++){
System.out.println(getBank().getCustomer(i).getNumOfAccounts());
os.writeObject(getBank().getCustomer(i));
}// 将User对象写进文件
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} 展开
Customer[] cus=new Customer[100];
Customer c;
try {
ObjectInputStream is = new ObjectInputStream(new FileInputStream(
"wxps.txt"));
bankInstance=(Bank)is.readObject();
for(int i=0;i<getBank().getNumOfCustomers();i++){
cus[i]=(Customer) is.readObject();
}
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return cus;
}
public static void ssBank(){
try {
ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream("wxps.txt"));
os.writeObject(bankInstance);
for(int i=0;i<getBank().getNumOfCustomers();i++){
System.out.println(getBank().getCustomer(i).getNumOfAccounts());
os.writeObject(getBank().getCustomer(i));
}// 将User对象写进文件
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} 展开
展开全部
因为不知道你的Bank和Customer是怎么实现的。所以不容易判断错误的原因。我根据你的情况写了并没有问题。给你参考下吧
import java.io.Serializable;
import java.util.Date;
public class Customer implements Serializable{
private String name;
private String surName;
private double balance;
private Date birthDay;
public String getName() {
return name;
}
public Customer setName(String name) {
this.name = name;
return this;
}
public String getSurName() {
return surName;
}
public Customer setSurName(String surName) {
this.surName = surName;
return this;
}
public double getBalance() {
return balance;
}
public Customer setBalance(double balance) {
this.balance = balance;
return this;
}
public Date getBirthDay() {
return birthDay;
}
public Customer setBirthDay(Date birthDay) {
this.birthDay = birthDay;
return this;
}
public void print(){
System.out.println("Customer name : "+name+", sur_name : "+surName+" with balance : "+balance);
}
}
import java.io.*;
public class Bank implements Serializable{
private String name;
private String address;
private Customer[] customers;
public Bank(final int numOfCustomers){
customers = new Customer[numOfCustomers];
}
public String getName() {
return name;
}
public Bank setName(String name) {
this.name = name;
return this;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Customer[] getCustomers() {
return customers;
}
public void setCustomers(Customer[] custoemrs) {
this.customers = custoemrs;
}
public Customer getCustomer(final int index){
return index < 0 || index >= customers.length ? null : customers[index];
}
public int getNumOfCustomers(){
return customers.length;
}
public void writeFile(final File file) throws IOException {
final ObjectOutputStream fout = new ObjectOutputStream(new FileOutputStream(file));
fout.writeObject(this);
fout.flush();
fout.close();
}
public Bank setCustomer(final Customer customer, final int index){
if(index < 0 || index >= customers.length){
return this;
} else {
customers[index] = customer;
return this;
}
}
public void print(){
System.out.println("Bank name : "+name+" with " + customers.length+" customers\n");
for(final Customer c : customers){
c.print();
}
}
public static Bank readFromFile(final File file) throws IOException, ClassNotFoundException {
final ObjectInputStream fin = new ObjectInputStream(new FileInputStream(file));
final Bank ret = (Bank) fin.readObject();
fin.close();
return ret;
}
}
import java.io.File;
import java.io.IOException;
import java.util.Date;
public class TestBank {
public static void main(String[] args) throws IOException, ClassNotFoundException {
final File file = new File("/Users/My/Desktop/bank.data");
final Bank b = new Bank(2);
b.setName("BOC")
.setCustomer(new Customer().setBalance(100.0).setBirthDay(new Date()).setName("Test").setSurName("My"), 0)
.setCustomer(new Customer().setBalance(200.0).setBirthDay(new Date()).setName("T1").setSurName("My2"), 1)
.writeFile(file);
b.print();
System.out.println("----------------------before read---------------");
final Bank b1 = Bank.readFromFile(file);
b1.print();
}
}
用这三个类试试,希望可以帮到你
2015-05-19
展开全部
读到最后,就报EOF错的。。。。表示读完了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询