Java File Name Get getFileName(String filePath)

Here you can find the source of getFileName(String filePath)

Description

get the file displayName from a file path;

License

Open Source License

Parameter

Parameter Description
filePath a parameter

Declaration

public static String getFileName(String filePath) 

Method Source Code


//package com.java2s;
import java.io.*;

public class Main {
    /**/*from w w w.  ja  v  a 2 s  .  c o m*/
     * get the file displayName from a file path;
     * @param filePath
     * @return
     */
    public static String getFileName(String filePath) {
        if (filePath == null) {
            return null;
        } else {
            int index = filePath.lastIndexOf('\\');
            if (index >= 0) {
                return filePath.substring(index + 1);
            } else {
                index = filePath.lastIndexOf('/');
                if (index >= 0) {
                    return filePath.substring(index + 1);
                } else {
                    return filePath;
                }
            }
        }
    }

    /**
     * get the displayName of a file; not including the file's path;
     *
     * @param file
     * @return
     */
    public static String getFileName(File file) {
        if (file == null) {
            return null;
        } else {
            return getFileName(file.getAbsolutePath());
        }
    }
}

Related

  1. getFileName(String completePath)
  2. getFileName(String dir, String name)
  3. getFileName(String f)
  4. getFileName(String fileName)
  5. getFileName(String filePath)
  6. getFileName(String filepath)
  7. getFileName(String filePath)
  8. getFileName(String filePath)
  9. getFilename(String filePath)