Java File Name Extract extractFileName(String fn)

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

Description

extract File Name

License

Apache License

Declaration

public static String extractFileName(String fn) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String extractFileName(String fn) {
        int j = lastIndexOfPathSeparator(fn);
        if (j == -1) {
            return fn;
        }// www  .  j a  va 2  s. com
        return (fn.substring(j + 1));
    }

    public static String extractFileName(String fn, boolean includeExt) {
        int j = lastIndexOfPathSeparator(fn);
        String s;
        if (j == -1) {
            s = fn;
        } else {
            s = fn.substring(j + 1);
        }
        if (!includeExt) {
            return excludeFileExt(s);
        }
        return s;
    }

    public static int lastIndexOfPathSeparator(String str) {
        if (str == null)
            return -1;

        int length = str.length();
        for (int i = length - 1; i >= 0; i--) {
            char c = str.charAt(i);
            if (c == '/' || c == '\\') {
                return i;
            }
        }

        return -1;
    }

    public static final String excludeFileExt(String fn) {
        if (fn == null)
            return fn;
        int i = lastIndexOf(fn, '.');
        if (i != -1) {
            fn = fn.substring(0, i);
        }
        return fn;
    }

    public static int lastIndexOf(String str, char ch) {
        if (str == null)
            return -1;

        int length = str.length();
        for (int i = length - 1; i >= 0; i--) {
            if (str.charAt(i) == ch) {
                return i;
            }
        }

        return -1;
    }
}

Related

  1. extractFileName(StackTraceElement aFrame)
  2. extractFileName(String file)
  3. extractFileName(String fileName)
  4. extractFileName(String filePathName)
  5. extractFileName(String filePathName)
  6. extractFileName(String fullPath)
  7. extractFileName(String name)
  8. extractFileName(String path)
  9. extractFileName(String path)