黑马程序员技术交流社区

标题: 获取文件扩展名 [打印本页]

作者: 一步步    时间: 2013-11-15 15:38
标题: 获取文件扩展名
请问获取文件扩展名的函数,都有什么方法,那种最好啊?

作者: 黑夜里的白猫    时间: 2013-11-15 16:08
可不可以 把字符串split一下,然后取到其扩展名。
作者: hel    时间: 2013-11-15 16:16
当让是endwith()最好啦
只需传一个字符串,皆可以啦

比如:以 .java结尾的文件 endwith(.java) 方便
作者: FFF    时间: 2013-11-15 17:48
如果问题已经解决,请及时修改主题为“提问结束”。
修改主题的方法链接
http://bbs.itheima.com/thread-89313-1-1.html
如果没有解决,可能你的问题问得不够清楚。可以重新发问的哦~
作者: 一步步    时间: 2013-11-15 18:42
hel 发表于 2013-11-15 16:16
当让是endwith()最好啦
只需传一个字符串,皆可以啦

如果是.txt   .word   .exce那我还得都写一遍?
作者: 阿里策    时间: 2013-11-15 19:28
本帖最后由 阿里策 于 2013-11-15 19:39 编辑
  1. class GetLast{
  2.         public static void main(String[] args)throws Exception{
  3.                 File file = new File("ttt.java");
  4.                 get(file);
  5.         }
  6.        
  7.         public static void get(File file){
  8.                         String name = file.getName();//先获取文件的名字
  9.                         String mid = name.substring(name.lastIndexOf('.'));//从文件名字字符串的最后一个.开始往后获取该名字的子串,就是扩展名了
  10.                         System.out.println(mid);
  11.         }
  12. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2