title: How to get Java File absolute path from InputStream?
body: <p>I'm on Java 6 and I have a method that scans the runtime classpath for a file called <code>config.xml</code>. If found, I would like to read the contents of the file into a string:</p>
  
  <pre><code>InputStream istream = this.getClass().getClassLoader().getResourceAsStream("config.xml");
  if(istream != null) {
      System.out.println("Found config.xml!");
  
      StringBuffer fileData = new StringBuffer(1000);
      BufferedReader reader;
      try {
          reader = new BufferedReader(new FileReader(fileName));
          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 fnfExc) {
          throw new RuntimeException("FileNotFoundException: " + fnfExc.getMessage());
      } catch (IOException ioExc) {
          throw new RuntimeException("IOException: " + ioExc.getMessage());
      }
  }
  </code></pre>
  
  <p>When I run this code, I get the following console output:</p>
  
  <pre><code>Found config.xml!
  Exception in thread "main" java.lang.RuntimeException: FileNotFoundException: config.xml (No such file or directory)
      at com.me.myapp.Configurator.readConfigFileFromClasspath(Configurator.java:556)
      at com.me.myapp.Configurator.&lt;init&gt;(Configurator.java:34)
      ...rest of stack trace omitted for brevity
  </code></pre>
  
  <p>So the classpath scan for <code>config.xml</code> is successful, but then the reader can't seem to find the file. <strong>Why???</strong> My only theory is that when <code>config.xml</code> is found on the classpath, it doesn't contain an absolute path to the location of the file on the file system, and perhaps that's what the reader code is looking for.</p>