欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2023-12-22 21:48:40
...

来源网址: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);
    }
}
相关标签: lastIndexOf()

上一篇:

下一篇: