Java Path File Name nio getFileNameFromPath(String str)

Here you can find the source of getFileNameFromPath(String str)

Description

parse the file name from file path string

License

Open Source License

Parameter

Parameter Description

Return

file name

Declaration

public static String getFileNameFromPath(String str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    /**//from   w ww .  j  ava2 s  .co  m
     * parse the file name from file path string
     *
     * @param str:file path contains filename
     * @return file name
     */
    public static String getFileNameFromPath(String str) {
        if (str == null || str.equals("") || str.isEmpty()) {
            return null;
        }
        Path p = Paths.get(str);
        String fileName = p.getFileName().toString();
        return fileName;
    }

    public static String toString(double[] p, String token) {
        String s = "";
        for (int i = 0; i < p.length; i++) {
            s += p[i] + token;
        }
        return s;
    }

    public static String toString(float[] p, String token) {
        String s = "";
        for (int i = 0; i < p.length; i++) {
            s += p[i] + token;
        }
        return s;
    }

    public static String toString(long[] p, String token) {
        String s = "";
        for (int i = 0; i < p.length; i++) {
            s += p[i] + token;
        }
        return s;
    }

    public static String toString(int[] p, String token) {
        String s = "";
        for (int i = 0; i < p.length; i++) {
            s += p[i] + token;
        }
        return s;
    }

    public static String toString(short[] p, String token) {
        String s = "";
        for (int i = 0; i < p.length; i++) {
            s += p[i] + token;
        }
        return s;
    }

    public static String toString(byte[] p, String token) {
        String s = "";
        for (int i = 0; i < p.length; i++) {
            s += p[i] + token;
        }
        return s;
    }

    public static String toString(char[] p, String token) {
        String s = "";
        for (int i = 0; i < p.length; i++) {
            s += p[i] + token;
        }
        return s;
    }

    public static String toString(boolean[] p, String token) {
        String s = "";
        for (int i = 0; i < p.length; i++) {
            s += p[i] + token;
        }
        return s;
    }

    public static String toString(String[] p, String token) {
        String s = "";
        for (int i = 0; i < p.length; i++) {
            s += p[i] + token;
        }
        return s;
    }

    /**
     * parse the file name from composite file structure/name
     *
     * @param str:file path contains filename
     * @return file name
     */
    public static String getFileName(String str) {
        String extension = str.substring(str.lastIndexOf('.') + 1);
        String fileName = getFileNameFromPath(str);
        if (extension.equals(fileName)) {
            return fileName;
        } else {
            String ret = fileName.substring(0,
                    fileName.lastIndexOf(extension) - 1);
            return ret;
        }
    }
}

Related

  1. getFileName(Path path)
  2. getFileName(String fullPath, boolean withParentFolder)
  3. getFileName(String path)
  4. getFileName(String relativePath)
  5. getFileNameFromPath(String filePath)
  6. getFilenameOrLastPath(String path)
  7. getFileNames(List fileNames, Path dir)
  8. getFileNames(List fileNames, Path dir, String ext, boolean recursive)
  9. getFileNames(List fileNames, Path dir, String sFilter)