从servlet写入对象到文件

时间:2015-07-27 12:20:54

标签: java file exception servlets

我试图从servlet写入文件,我不确定我在xml文件中给出的路径是错误还是别的

    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;
}

我用来从文件中读取的方法

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();

}

和xml param

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

那是例外

at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at service.UsersService.getUsersList(UsersService.java:48)
at service.UsersService.<init>(UsersService.java:29)
at controller.LogServlet.doPost(LogServlet.java:43)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at controller.LogFilter.doFilter(LogFilter.java:60)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

这里我们说如果没有更多的字节需要读取,那么我们应该在尝试读取对象之前突破while循环等。

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