java - write object to file from servlet -


i'm trying write file servlet , i'm not sure if path gave in xml file wrong or else

    private list<user> getuserslist() throws ioexception, classnotfoundexception{     list<user> users=new arraylist<user>();      fileinputstream fis=new fileinputstream(f);     objectinputstream ois=new objectinputstream(fis);     user u;     while(true){         if((u=(user) ois.readobject())==null)             break;         else{             users.add(u);         }     }     ois.close();     return users; } 

that's method use read file where

private final string usersfile; private arraylist<user> users; private file f; public usersservice(string filename) throws classnotfoundexception, ioexception {     super();     usersfile=filename;     f=new file(this.usersfile);     if(!f.exists()){         f.createnewfile();     }     users= (arraylist<user>) this.getuserslist();  } 

and xml param

 <context-param> <param-name>userslist</param-name> <param-value>/users.txt</param-value> </context-param> 

and that's exception

at java.io.objectinputstream$blockdatainputstream.peekbyte(unknown source) @ java.io.objectinputstream.readobject0(unknown source) @ java.io.objectinputstream.readobject(unknown source) @ service.usersservice.getuserslist(usersservice.java:48) @ service.usersservice.<init>(usersservice.java:29) @ controller.logservlet.dopost(logservlet.java:43) @ javax.servlet.http.httpservlet.service(httpservlet.java:647) @ javax.servlet.http.httpservlet.service(httpservlet.java:728) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ controller.logfilter.dofilter(logfilter.java:60) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:243) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:51) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:243) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:222) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:123) @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:502) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:100) @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:953) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408) @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1041) @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:603) @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:312) @ java.util.concurrent.threadpoolexecutor.runworker(unknown source) @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source) @ java.lang.thread.run(unknown source) 

here if there aren't more bytes read should break out of while loop before trying read object, etc.

 while (true) {    if (ois.readobject() == -1) break;    //...rest of code } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -