title: Why I'm getting different results using url and absolute file name?
body: <p>I am new in Java and facing a problem aceessing a file.
  I have a package named "FileOperation" uses a "boom.txt" file(Location: "FileOperation/files/boom.txt"). What I wanted to do was to access the file from a class "MyTextPanel" (Location: "FileOperation/MyTextPanel.java"). </p>
  
  <p>I have created 2 methods for this purpose. One is by getClass.getResource(path) and other is by giving the absolute location of the file. Second method works, first doesn't. Someone please explain, what's the problem with the first method. Here is the class MyTextPanel:</p>
  
  <pre><code>package FileOperaton;
  import java.awt.BorderLayout;
  import java.io.FileReader;
  import java.net.URL;
  
  import javax.swing.JPanel;
  import javax.swing.JTextArea;
  
  
  public class MyTextPanel extends JPanel {
  
      JTextArea textArea;
      MyTextPanel()
      {
          setLayout(new BorderLayout());
          textArea=new JTextArea();
          add(textArea,BorderLayout.CENTER);
  
          String fileName;
          fileName=getFileName1();
          //fileName=getFileName2();
          System.out.println("File Name = "+fileName);
          if(fileName!=null)
          {
              try {
                      FileReader fr=new FileReader(fileName);
                      System.out.println("Access Successful");
              } catch (Exception e) {
                      e.printStackTrace();
              }
          }
      }
  
      private String getFileName1()
      {
          URL url=getClass().getResource("/files/boom.txt");
          System.out.println("by getFileName1()");
          if(url!=null)
              return url.getFile();
          return null;
      }
  
      private String getFileName2()
      {
          String absolutePath="E:\\Project Eclipse\\Workspace\\FileOperation\\src\\FileOperaton\\files\\boom.txt";
          System.out.println("by getFileName2()");
          return absolutePath;
      }
  }
  </code></pre>
  
  <p>And here are the outputs:</p>
  
  <pre><code>by getFileName1()
  File Name =/E:/Project%20Eclipse/Workspace/FileOperation/bin/FileOperaton/files/boom.txt
  java.io.FileNotFoundException: E:\Project%20Eclipse\Workspace\FileOperation\bin\FileOperaton\files\boom.txt (The system cannot find the path specified)
      at java.io.FileInputStream.open(Native Method)
      at java.io.FileInputStream.&lt;init&gt;(Unknown Source)
      at java.io.FileInputStream.&lt;init&gt;(Unknown Source)
      at java.io.FileReader.&lt;init&gt;(Unknown Source)
      at FileOperaton.MyTextPanel.&lt;init&gt;(MyTextPanel.java:32)
      at FileOperaton.MyFrame.&lt;init&gt;(MyFrame.java:22)
      at FileOperaton.MyFrame.main(MyFrame.java:29)
  </code></pre>
  
  <p>by second method:</p>
  
  <pre><code>by getFileName2()
  File Name = E:\Project Eclipse\Workspace\FileOperation\src\FileOperaton\files\boom.txt
  Access Successful
  </code></pre>
  
  <p>I've also tried url=getClass().getResource("/files/boom.txt") in the first method but there url is assigned as null.
  And in the second method, if I use absolutePath="E:\Project Eclipse\Workspace\FileOperation\bin\FileOperaton\files\boom.txt"; (bin instead of src) it also gets success.</p>