这篇文章将为大家详细讲解有关怎么通过java获取文件名和扩展名,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
成都创新互联公司专注于网站建设|网页维护|优化|托管以及网络推广,积累了大量的网站设计与制作经验,为许多企业提供了网站定制设计服务,案例作品覆盖办公窗帘等行业。能根据企业所处的行业与销售的产品,结合品牌形象的塑造,量身定制品质网站。
如:文件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/article38/pigjsp.html
成都网站建设公司_创新互联,为您提供定制开发、域名注册、手机网站建设、网站维护、企业网站制作、商城网站
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联