Java File Name Extract extractFileName(String file)

Here you can find the source of extractFileName(String file)

Description

extract File Name

License

Open Source License

Declaration

static String extractFileName(String file) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w.ja v a  2s .c  o m*/
 * JBoss, Home of Professional Open Source.
 *
 * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
 *
 * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
 */

public class Main {
    static String extractFileName(String file) {

        if (file.matches("^(\\w){2,}:.*")) // Handles URLs - No conversion necessary //$NON-NLS-1$
            // http://lib/foo.txt - currently do not support, converts to local
            // host with absolute path
            // file://lib/foo.txt
            // file:///c:/lib/foo.txt
            return null;
        else if (file.matches("^\\/.*")) // Handles absolute paths- it can be file or URL depending upon //$NON-NLS-1$
            // context Conversion needed
            // /lib/foo.txt
            return file;
        else if (file.matches("^\\w:[\\\\,\\/].*")) { //$NON-NLS-1$
            // Handles windows absolute path - no conversion needed
            // c:\\lib\\foo.txt
            // c:/lib.foo.txt
            file = file.replaceAll("\\\\", "\\/"); //$NON-NLS-1$ //$NON-NLS-2$
            return "/" + file; //$NON-NLS-1$
        } else if (file.matches("^(\\.)+\\/.*|^\\w+\\/.*|^\\w+.*")) // Handles relative paths - these can be URLs or files - //$NON-NLS-1$
            // conversion necessary
            // ./lib/foo.txt
            // ../lib/foo.txt
            // lib/foo.txt
            return file;
        return null;
    }
}

Related

  1. extractFileName(final String filePath)
  2. extractFileName(StackTraceElement aFrame)
  3. extractFileName(String fileName)
  4. extractFileName(String filePathName)
  5. extractFileName(String filePathName)
  6. extractFileName(String fn)