title: How to read file on Windows and Linux from Java
body: <p>I have a xml file located in <code>D:\XML\RequestXML</code> and I am reading xml file in this folder from a <code>FileReader</code>. In my program I hard coded the file path <code>/XML/RequestXML/</code>. This works fine with the <code>windows</code> environment. In windows <code>JBoss</code> is in <code>D:\jbossdistrib\jboss</code>.</p>
  
  <p>I created the folder structure in <code>linux</code> <code>/usr/XML/RequestXML/</code>. And add the xml in to RequestXML folder.  <code>JBoss</code> is in <code>/usr/jbossdistrib/jboss/</code> path.</p>
  
  <p>But my application can not find the file specified in /XML/RequestXML/  in linux environment.</p>
  
  <p>If I change the file path as <code>/usr/XML/RequestXML/</code> it works in linux.</p>
  
  <p>How can I use the consistent file path in linux and windows both?</p>
  
  <pre><code>public class Controller extends HttpServlet {
  
    private String filePath = "/XML/RequestXML/";
  
    protected void doPost(HttpServletRequest request,
          HttpServletResponse response) throws ServletException, IOException {
  
         String file = request.getParameter("fileName");
  
         xml =  readFile(filePath + file);
  
      }
  
    private String readFile(String file) {
      StringBuffer fileData = new StringBuffer();
      try {
  
          BufferedReader reader = new BufferedReader(new FileReader(file));
          char[] buf = new char[1024];
          int numRead=0;
  
          while((numRead=reader.read(buf)) != -1){
              String readData = String.valueOf(buf, 0, numRead);
              fileData.append(readData);
              buf = new char[1024];
          }
          reader.close();
  
      }
      catch (FileNotFoundException e) {
          logger.fatal("File not found in specifid path "+ file);
      }
      catch (IOException e) {
          logger.fatal("Error while reading the xml file");
      }
      return fileData.toString();
   }
  }
  </code></pre>
  
  <hr>
  
  <p>Update</p>
  
  <p>My question is how to set the file path without <code>/usr/</code> which works fine in Windows. 
  If this is not possible, then do I need to use the path as <code>/usr/XML/RequestXML/</code> in windows environment as well? so I have to create a folder structure like <code>D:\usr\XML\RequestXML</code> in windows.</p>