Here you can find the source of getFileName(String filePath)
Parameter | Description |
---|---|
filePath | the file path as a string |
public static String getFileName(String filePath)
//package com.java2s; //License from project: Apache License import java.io.*; public class Main { /**//w ww . j ava 2 s. c o m * An OS independent getName alternative. Useful if the path is provided as * a hardcoded string and opened in a different OS. * * @param filePath the file path as a string * @return the file name, or the complete path of no file name is detected */ public static String getFileName(String filePath) { String tempFileName = filePath; int slash1 = tempFileName.lastIndexOf("/"); int slash2 = tempFileName.lastIndexOf("\\"); int lastSlashIndex = Math.max(slash1, slash2); if (lastSlashIndex != -1) { tempFileName = tempFileName.substring(lastSlashIndex + 1); } return tempFileName; } /** * An OS independent getName alternative. Useful if the path is provided as * a hardcoded string and opened in a different OS. * * @param file the file * @return the file name, or the complete path of no file name is detected */ public static String getFileName(File file) { return getFileName(file.getAbsolutePath()); } }