存取在application中,只要服务不重起就不会消失,看一看application的生命周期

解决方案 »

  1.   

    to biggie(飞碟) :
    我要的就是服务器重起之后还可以load进来
    否则很容易实现,没有必要写文件
      

  2.   

    还有,我实在没找到application在哪里,我只是写了一个Servlet而已
    不过,还是谢谢你的回复
      

  3.   

    在servlet中可以写文件啊,在servlet的init()函数中读出文件,在destroy函数中写入文件。import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;public class InitDestroyCounter extends HttpServlet {  int count;  public void init() throws ServletException {
        // Try to load the initial count from our saved persistent state
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
          fileReader = new FileReader("InitDestroyCounter.initial");
          bufferedReader = new BufferedReader(fileReader);
          String initial = bufferedReader.readLine();
          count = Integer.parseInt(initial);
          return;
        }
        catch (FileNotFoundException ignored) { }  // no saved state
        catch (IOException ignored) { }            // problem during read
        catch (NumberFormatException ignored) { }  // corrupt saved state
        finally {
          // Make sure to close the file
          try {
            if (bufferedReader != null) {
              bufferedReader.close();
            }
          }
          catch (IOException ignored) { }
        }    // No luck with the saved state, check for an init parameter
        String initial = getInitParameter("initial");
        try {
          count = Integer.parseInt(initial);
          return;
        }
        catch (NumberFormatException ignored) { }  // null or non-integer value    // Default to an initial count of "0"
        count = 0;
      }
      public void destroy() {
        super.destroy();  // entirely optional
        saveState();
      }  public void saveState() {
        // Try to save the accumulated count
        FileWriter fileWriter = null;
        PrintWriter printWriter = null;
        try {
          fileWriter = new FileWriter("InitDestroyCounter.initial");
          printWriter = new PrintWriter(fileWriter);
          printWriter.println(count);
          return;
        }
        catch (IOException e) {  // problem during write
          // Log the exception. See Chapter 5.
        }
        finally {
          // Make sure to close the file
          if (printWriter != null) {
            printWriter.close();
          }
        }
      }
    }