我正在尝试将POJO类的对象写入文件,并使用Java序列化方法使用相同的Servlet读取它们。我在公共课中有POJO,而在另外两个公共课中有作家和读者代码。然后我在servlet中访问了writer和reader方法。问题是只能写入文件的最后一个对象反序列化。看来之前写的对象被覆盖了。请参阅以下代码。
POJO课程:
public class Comment implements Serializable{
private String name;//max 24 digits
private String date;//6 digits
private String email;//max 24 digits
private String comment; //to be stored into database as clob no max
private String id;//auto generated
public Comment(){}
public Comment(String id){
this.id = id;
}
public Comment(String name, String email, String comment){
this.name=name;
this.date=date;
this.email=email;
this.comment=comment;
}
public Comment(String name, String date, String email, String comment){
this.name=name;
this.date=date;
this.email=email;
this.comment=comment;
}
public Comment(String name, String date, String email, String comment, String id){
this.name=name;
this.date=date;
this.email=email;
this.comment=comment;
this.id=id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the date
*/
public String getDate() {
return date;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the comment
*/
public String getComment() {
return comment;
}
/**
* @param comment the comment to set
*/
public void setComment(String comment) {
this.comment = comment;
}
Comment myComment;
@Override
public int hashCode()
{
return 31 + id.hashCode();
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Comment){
Comment another = (Comment)obj;
if(this.getId()==another.getId()){
return true;
}
}
return false;
}
public String toString(){
StringBuilder sb = new StringBuilder();
return sb.append(name).append(",").append(" ").append(date).append(",").append(" ").append(comment).append(",").append(" ").append(id).append(",").append(" ").append(email).toString();
}
public void setDate(String date) {
this.date = date;
}
}
作家班:
public class ObjectStreamWriterExample {
public void write(Object obj){
File outFile = new File("out.txt");
try(ObjectOutputStream objectOutput = new ObjectOutputStream(new FileOutputStream(outFile));){
List <Object> commentList = new ArrayList<>();
commentList.add(obj);
for(Object list : commentList){
objectOutput.writeObject(list);
//System.out.println("good ok now");
}
}catch(IOException io){
System.err.println("An Error occured :");
System.out.println(io.getCause());
}
}
}
读者课程:
public class ObjectStreamReaderExample {
public List <Comment> read( String inFile){
Comment comment = null;
List <Comment> list = new ArrayList<>();
try(ObjectInputStream oi = new ObjectInputStream(new FileInputStream(inFile));){
while(true){
try {
comment = (Comment)oi.readObject();
list.add(comment);
break;
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}catch(EOFException eof){
System.err.println("Reached End of File");
eof.printStackTrace();
}
}
}catch(IOException io){
System.err.println("Error :");
io.printStackTrace();
}
return list;
}
}
servlet:
public class ObjectStreamExample extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
ObjectStreamWriterExample oswe =new ObjectStreamWriterExample();
oswe.write(new Comment("Omobolaji", "omo@javanice.net", "omo is commenting"));
oswe.write(new Comment("Omobolaji", "omo@javanice.net", "omo is commenting again"));
oswe.write(new Comment("Omobolaji", "omo@javanice.net", "omo is commenting again hhhhh"));
ObjectStreamReaderExample osre = new ObjectStreamReaderExample();
List <Comment> list = new ArrayList<>();
list = osre.read("out.txt");
for(Comment myList : list){
out.println(myList.getName() + "<br> ");
out.println(myList.getEmail() + "<br> ");
out.println(myList.getComment() + "<br> ");
}
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
答案 0 :(得分:0)
没有神秘感。每次调用write()
时,都会创建一个新文件并向其写入一个对象。您还可以创建一个完全没有意义的列表,向其添加一个对象,迭代列表,当然,然后将您在其中找到的一个对象写入文件。下次打电话给write()
时,你再次做了所有这些废话。结果:一个文件,其中包含一个对象。
你需要完全重新考虑这一切。没有特殊措施,您无法附加到对象流文件。你应该:
EOFException
发出错误信号。或强>
NB
.txt
的文件中。