filename extension

来源:互联网 发布:安装linux u盘坏了 编辑:程序博客网 时间:2024/04/29 23:36
import java.util.Scanner;public class Main {    @SuppressWarnings("resource")    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        String filePath = scanner.nextLine();        System.out.println(Main.extensionOfFile(filePath));    }    /**     * 获取文件后缀名     *      * 题目描述     *      Please create a  function to extract the filename extension from      *      the given path,return the extracted filename extension or null  if none.     *      * @param path     * @return     */    public static String extensionOfFile(String path) {        if(path == null || path == "") return null;        int index = 0;        for(int i = path.length() - 1; i >= 0; i--) {            if(path.charAt(i) == '.') {              index = i;              break;            }        }        String extension = path.substring(index + 1, path.length());        return extension;    }}
import java.util.Scanner;public class Main {    @SuppressWarnings("resource")    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        String filePath = scanner.nextLine();        System.out.println(Main.extensionOfFile(filePath));    }    /**     * 获取文件后缀名     *      * 题目描述     *      Please create a  function to extract the filename extension from      *      the given path,return the extracted filename extension or null  if none.     *      * @param path     * @return     */    public static String extensionOfFile(String path) {        if(path == null || path == "") return null;        return path.substring(path.lastIndexOf('.')+1, path.length());    }}
阅读全文
0 0