• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

unable to read all objects from file

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

i developed sample application , that is write objects to file then read all objects into browser.

For this i have used ObjectInputStream in append mode. While retriving objects , it is thowing IOException and StreamCorruptException.

Please find the below java code and Error and please help me to resolve this issue.

java code
-----------
package com.test.controller;

import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.Resource;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.mvc.BaseCommandController;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.mvc.ParameterizableViewController;

import com.test.command.RegisterCommand;

public class RegisterController extends AbstractCommandController {

public String fileName = "users.dat";


FileOutputStream fos = null;
FileInputStream fis = null;
BufferedWriter bw = null;
FileWriter fw = null;
RegisterCommand cmdr = null;
PrintWriter out = null;
RegisterCommand cmd = null;
File file = new File("/test.dat");
ObjectInputStream ois = null;
StringBuffer sb = null;
String str = null;
public ModelAndView handle(
HttpServletRequest request,
HttpServletResponse response,Object command ,BindException errors) throws IOException {

out = response.getWriter();
cmd = (RegisterCommand)command;

try {
System.out.println("Command to execute :: " +request.getParameter("btn_show"));
if(request.getParameter("btn_submit")!=null && request.getParameter("btn_submit").toString()!="Register")
{
fos = new FileOutputStream(file,true);
ObjectOutputStream ost = new ObjectOutputStream(fos);

ost.writeObject(cmd);
out.println("object serialized sucessfully"+"<br>");
out.println("Employee Number ::" + cmd.getTxt_empno()+"<br>");
out.println("First Name :: "+cmd.getTxt_fname()+"<br>");
out.println("Last Name :: "+cmd.getTxt_lname()+"<br>");
out.println("Address ::"+cmd.getTxt_address()+"<br>");
out.println("Gender :: "+cmd.getGender()+"<br>");
ost.flush();
ost.close();
fos.close();
out.println("object serialized sucessfully"+"<br>");
}
else
if(request.getParameter("btn_show")!=null && request.getParameter("btn_show").toString()!="Show")
{


ois = new ObjectInputStream(new FileInputStream(file));

RegisterCommand obj = null;
while((obj=(RegisterCommand)(ois.readObject()))!=null)
{
System.out.println("Reading object..........****");
if(obj == null){
break;
}
out.println("Before Deserialization "+"<br>");
out.println("Employee Number ::" + obj.getTxt_empno()+"<br>");
out.println("First Name :: "+obj.getTxt_fname()+"<br>");
out.println("Last Name :: "+obj.getTxt_lname()+"<br>");
out.println("Address ::"+obj.getTxt_address()+"<br>");
out.println("Gender :: "+obj.getGender()+"<br>");
out.println("Object desiarilized successfully");


}
ois.close();

}

}
catch(EOFException ex)
{
ex.printStackTrace();
}
catch (FileNotFoundException e)
{
System.out.println("FileNotFoundException :: "+ e.getMessage());
e.printStackTrace();
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
catch(IOException e)
{
System.out.println("IOException :: " +e.getMessage());
e.printStackTrace();
}
finally
{/*
try {
if (ois != null) {
ois.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}*/
}

// ModelAndView mav = new ModelAndView("null","message" ,message);
// mav.addObject("message", "Hello World!");


return null;
}
}


Exception
------------
Command to execute :: null
Command to execute :: null
Command to execute :: Show
Reading object..........****
IOException :: null
java.io.StreamCorruptedException
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.test.controller.RegisterController.handle(RegisterController.java:85)
at org.springframework.web.servlet.mvc.AbstractCommandController.handleRequestInternal(AbstractCommandController.java:84)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic