Java中如何获取文件名以及的文件的后缀名

来源网址:https://www.cnblogs.com/henuyuxiang/p/7485834.html


import java.io.File;

public class Test {
    public static void main(String[] args) {
        File file = new File("HelloWorld.java");
        String fileName = file.getName();
        String fileNameWithoutSuffix = fileName.substring(0,fileName.lastIndexOf("."));//获取没有后缀的文件名
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);//获取文件的后缀名
        //beginIndex:索引值加1,意味着从“.”的后一位元素开始;endIndex:没有写结束索引值意味着获取到最后一个元素。
        System.out.println("没有后缀的文件名:"+ fileNameWithoutSuffix + "    文件的后缀名:"+ suffix);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43457486/article/details/85158203