extract file name form the given file path - Android java.io

Android examples for java.io:File Path

Description

extract file name form the given file path

Demo Code

import android.graphics.Paint;
import android.graphics.Paint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;

public class Main{

    /**/*from   w w w  .j  a  v a2 s.  c  om*/
     * extract file name form the given file path
     * 
     * @param filePath
     *            path to the file, like 'c:/test.jpg', 'c:\\test.jpg'
     * @param withExtention
     *            indicate contain file.extention. true : contain | false :
     *            ignore
     * @return fileName file.name;
     */
    public static String getFileName(String filePath, boolean withExtention) {
        int sep = filePath.lastIndexOf("\\") == -1 ? filePath
                .lastIndexOf("/") : filePath.lastIndexOf("\\");
        if (withExtention)
            return filePath.substring(sep + 1);
        return filePath.substring(sep + 1, filePath.lastIndexOf("."));
    }

}

Related Tutorials