title: Java can't get the path of a file that exists in the current directory
body: <p>If a file exists in the same directory where a Java application is running and I create a File object for that file the Java File methods for the path of the file include the filename as well.  Code and output are below.</p>
  
  <p>If this was a bug in the JDK version I'm using someone would surely have seen it by now.</p>
  
  <p>Why do File.getAbsolutePath() and File.getCanonicalPath() include the file name?  The Javadocs indicate that the directory name should be returned.</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  
  
  public class DirectoryFromFile {
  
      private void getDirectoryOfFile(String fileName) throws IOException{
  
          File f = new File(fileName );
  
          System.out.println("exists(): " + f.exists());
          System.out.println("getPath(): " + f.getPath());
          System.out.println("getAbsolutePath(): " + f.getAbsolutePath());
          System.out.println("getParent(): " + f.getParent() );
          System.out.println("getCanonicalPath(): " + f.getCanonicalPath() );
          System.out.println("getAbsoluteFile().getCanonicalPath(): " + f.getAbsoluteFile().getCanonicalPath() );
          String dirname = f.getCanonicalPath();
          System.out.println("dirname: " + dirname);
          File dir = new File(dirname);
          System.out.println("dir: " + dir.getAbsolutePath());
  
          if (dirname.endsWith(fileName))
              dirname = dirname.substring(0, dirname.length() - fileName.length());
          System.out.println("dirname: " + dirname);
          File dir2 = new File(dirname);
          System.out.println("dir2: " + dir2.getAbsolutePath());
      }
  
      public static void main(String[] args) {
  
          DirectoryFromFile dff = new DirectoryFromFile();
          try {
              dff.getDirectoryOfFile("test.txt");
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p>Here' the output:</p>
  
  <pre><code>exists(): true
  getPath(): test.txt
  getAbsolutePath(): C:\dean\src\java\directorytest\directory.from.file\test.txt
  getParent(): null
  getCanonicalPath(): C:\dean\src\java\directorytest\directory.from.file\test.txt
  getAbsoluteFile().getCanonicalPath(): C:\dean\src\java\directorytest\directory.from.file\test.txt
  dirname: C:\dean\src\java\directorytest\directory.from.file\test.txt
  dir: C:\dean\src\java\directorytest\directory.from.file\test.txt
  dirname: C:\dean\src\java\directorytest\directory.from.file\
  dir2: C:\dean\src\java\directorytest\directory.from.file
  </code></pre>
  
  <hr>
  
  <p>So far the only way I've found to get the directory in this case is to manually parse off the file name.  </p>
  
  <p>Does the File class have a way to get the directory name in this case (where a File that exists in the current directory is created without specifying a directory)?</p>