java中怎么获取文件名和扩展名

本篇文章为大家展示了java中怎么获取文件名和扩展名,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

同安网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、响应式网站开发等网站项目制作,到程序开发,运营维护。创新互联建站从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站

如:文件filePath = "E:\\test\\test.dxf"

1.获取文件名

eg:获取 test.dxf

通过file对象

import java.io.File;public class test {  public static void main(String[] args) {    String filePath = "E:\\test\\test.dxf";    File tmpFile=new File(filePath);    String fileName=tmpFile.getName();    System.out.println(fileName);  }}

使用split

public class test {  public static void main(String[] args) {    String filePath = "E:\\test\\test.dxf";    //带扩展名的文件名    String temp[] = filePath.split("\\\\");    String fileName = temp[temp.length - 1];    System.out.println(fileName);  }}

使用substring

public class test {  public static void main(String[] args) {    String filePath = "E:\\test\\test.dxf";    String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);    System.out.println(fileName);  }}

2.获取不带扩展名的文件名

eg:获取 test

使用substring

public class test {  public static void main(String[] args) {    String filePath = "E:\\test\\test.dxf";    String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);    String name = fileName.substring(0,fileName.lastIndexOf("."));    System.out.println(name);  }}

3.扩展名

eg:获取 dxf

使用substring

public class test {  public static void main(String[] args) {    String filePath = "E:\\test\\test.dxf";    String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);    String name = fileName.substring(filePath.lastIndexOf(".")+1);    System.out.println(name);  }}

public class test {  public static void main(String[] args) {    String filePath = "E:\\test\\test.dxf";    String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);    String[] strArray = fileName.split("\\.");    int suffixIndex = strArray.length -1;    System.out.println(strArray[suffixIndex]);  }}

public class test {  public static void main(String[] args) {    String filePath = "E:\\test\\test.dxf";    String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);    System.out.println(fileName);    String extension=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());    System.out.println(extension);  }}

上述内容就是java中怎么获取文件名和扩展名,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。

本文标题:java中怎么获取文件名和扩展名
当前网址:https://www.cdcxhl.com/article6/gsjiog.html

成都网站建设公司_创新互联,为您提供App设计定制开发域名注册网站导航Google软件开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

商城网站建设