所以我在这里尝试做的是从文件中加载一个Customer对象,但我不知道如何在方法中返回该对象。正如代码现在所示我得到一个错误,说返回类型不兼容java.lang.object找到需要Customer。
public static Customer loadCustomer(String customerNum) {
try {
String fileName = customerNum + ".txt";
InputStream file = new FileInputStream(fileName);
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream(buffer);
Object x = input.readObject();
return x;
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
我以为我可以做这样的事情,但它也不起作用。
public static Customer loadCustomer(String customerNum) {
try {
String fileName = customerNum + ".txt";
InputStream file = new FileInputStream(fileName);
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream(buffer);
Object x = input.readObject();
Customer y = new Customer(x);
return y;
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
//or do something like this:Customer x = new Customer(input.readObject)
我还在java学习过程中,所以如果你看到任何其他错误,请告诉我。一如既往,我非常感谢每个人有时间帮助我解决这个问题,期待有一天我有知识向他们付钱并帮助他人。
答案 0 :(得分:0)
尝试将Object x = input.readObject();
替换为Customer x = (Customer) input.readObject();