title: Java Servlet - write data to file
body: <p>I have a servlet which uses file with data. The relative path to this file is contained in web.xml.
  I have following part of code, which reads data from file:</p>
  
  <pre><code>public class LoginServlet extends HttpServlet {
  private Map&lt;String, UserData&gt; users;
  public void init() throws ServletException {
      super.init();
      String userFilePath = getServletContext().getInitParameter("user.access.file");
      InputStream userFile = this.getClass().getResourceAsStream(userFilePath);
      try {
          users = readUsersFile(userFile);
      } catch (IOException e) {
          e.printStackTrace();
          throw new ServletException(e);
      }
              ....
              ....
      }
  
  private Map&lt;String, UserData&gt; readUsersFile(InputStream is) throws IOException{
      BufferedReader fileReader = new BufferedReader(new InputStreamReader(is));
      Map&lt;String, UserData&gt; result = new HashMap&lt;String, UserData&gt;();
              ....
              ....
              ....
              return result;
       }
  }
  </code></pre>
  
  <p>Because this is a servlet and it will not work only on my PC, I can't use absolute path.
  Does anyone know how I can write data to the file, using a similar way?</p>