commons-file/src/main/java/info/monitorenter/util/FileUtil.java
public static Map.Entry<String, String> cutDirectoryInformation(final String path) {
    final StringBuffer dir = new StringBuffer();
    String file = "";
    final String fileseparator = System.getProperty("file.separator");
    final StringTokenizer tokenizer = new StringTokenizer(path, fileseparator);
    final int size = tokenizer.countTokens();
    switch (size) {
      case 0:
        dir.append(new File(".").getAbsolutePath());
        break;

      case 1:
        final File test = new File(tokenizer.nextToken());
        if (new File(path).isDirectory()) {
          dir.append(test.getAbsolutePath());
        } else {
          dir.append(new File(".").getAbsolutePath());
          file = path;
        }
        break;

      default:
        String token;
        while (tokenizer.hasMoreElements()) {
          // reuse String file separator: bad style...
          token = tokenizer.nextToken();
          if (tokenizer.hasMoreTokens()) {
            dir.append(token);
            dir.append(fileseparator);
          } else {
            if (new File(path).isFile()) {
              file = token;
            } else {
              dir.append(token);
            }
          }
        }
    }

    return new Entry<String, String>(dir.toString(), file);
  }
public static Map.Entry<String, String> cutDirectoryInformation(final String path) {
    final StringBuffer dir = new StringBuffer();
    String file = "";
    final String fileseparator = System.getProperty("file.separator");
    final StringTokenizer tokenizer = new StringTokenizer(path, fileseparator);
    final int size = tokenizer.countTokens();
    switch (size) {
      case 0:
        dir.append(new File(".").getAbsolutePath());
        break;

      case 1:
        final File test = new File(tokenizer.nextToken());
        if (new File(path).isDirectory()) {
          dir.append(test.getAbsolutePath());
        } else {
          dir.append(new File(".").getAbsolutePath());
          file = path;
        }
        break;

      default:
        String token;
        while (tokenizer.hasMoreElements()) {
          // reuse String file separator: bad style...
          token = tokenizer.nextToken();
          if (tokenizer.hasMoreTokens()) {
            dir.append(token);
            dir.append(fileseparator);
          } else {
            if (new File(path).isFile()) {
              file = token;
            } else {
              dir.append(token);
            }
          }
        }
    }

    return new Entry<String, String>(dir.toString(), file);
  }
public static Map . Entry < String , String > cutDirectoryInformation ( final String path ) { final StringBuffer dir = new StringBuffer ( ) ; String file = "" ; final String fileseparator = System . getProperty ( "file.separator" ) ; final StringTokenizer tokenizer = new StringTokenizer ( path , fileseparator ) ; final int size = tokenizer . countTokens ( ) ; switch ( size ) { case 0 : dir . append ( new File ( "." ) . getAbsolutePath ( ) ) ; break ; case 1 : final File test = new File ( tokenizer . nextToken ( ) ) ; if ( new File ( path ) . isDirectory ( ) ) { dir . append ( test . getAbsolutePath ( ) ) ; } else { dir . append ( new File ( "." ) . getAbsolutePath ( ) ) ; file = path ; } break ; default : String token ; while ( tokenizer . hasMoreElements ( ) ) { // reuse String file separator: bad style... token = tokenizer . nextToken ( ) ; if ( tokenizer . hasMoreTokens ( ) ) { dir . append ( token ) ; dir . append ( fileseparator ) ; } else { if ( new File ( path ) . isFile ( ) ) { file = token ; } else { dir . append ( token ) ; } } } } return new Entry < String , String > ( dir . toString ( ) , file ) ; }
Cuts the path information of the String that is interpreted as a filename
into the directory part and the file part. The current operating system's
path separator is used to cut all path information from the String.
<p>

<pre>

&quot;c:/work/programming/anyfile.jar&quot; --&gt; Map.Entry(&quot;c:/work/programming/&quot;,&quot;anyfile.jar&quot;);
&quot;anyfile.jar&quot;                     --&gt; Map.Entry(new File(&quot;.&quot;).getAbsolutePath(),&quot;anyfile.jar&quot;);
&quot;c:/directory1/directory2/&quot;       --&gt; Map.Entry(&quot;c:/directory1/directory2/&quot;,&quot;&quot;);
&quot;c:/directory1/directory2&quot;        --&gt; Map.Entry(&quot;c:/directory1/directory2/&quot;,&quot;&quot;); // directory2 is a dir!
&quot;c:/directory1/file2&quot;             --&gt; Map.Entry(&quot;c:/directory1/&quot;,&quot;file2&quot;);       // file2 is a file!
&quot;c:/&quot;                             --&gt; Map.Entry(&quot;c:/&quot;,&quot;&quot;);

</pre>

Assuming, that '/' is the current file separator character.
<p>

<b>If your string is retrieved from an <tt>URL</tt> instance, use
<tt>cutDirectoryInformation(URL path)</tt> instead, because URL's do not
depend on the operating systems file separator! </b>
<p>

@param path
the absolute file path you want the mere file name of.

@return the <code>{@link java.util.Map.Entry}</code> consisting of path
information and file name.
Cuts the path information of the String that is interpreted as a filename into the directory part and the file part . The current operating system s path separator is used to cut all path information from the String . <p >