资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么通过java获取文件名和扩展名

这篇文章将为大家详细讲解有关怎么通过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获取文件名和扩展名
当前链接:http://cdkjz.cn/article/pigjsp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220