A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© freeboyhrk 中级黑马   /  2013-4-11 10:15  /  2092 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 freeboyhrk 于 2013-4-11 14:11 编辑

如题,注意,是获取已知文件扩展名,首先怎么获取已知文件名?

8 个回复

倒序浏览
我是用截取的方法,然后数组最后一个装的就是扩展名,详细代码如下
  1. import java.io.*;
  2. class getendName
  3. {
  4.         public static void main(String []args){
  5.                 //获取已知文件
  6.                 File file = new File("C://sdasda.java");
  7.                 //获取已知文件名
  8.                 String filename = file.getName();
  9.                 //打印已知文件名
  10.                 System.out.println(filename);
  11.                 //用split()方法按  "."  截取文件.
  12.                 String name[] = filename.split("\\.");
  13.                 //最后一个数组装的就是文件扩展名
  14.                 String endname = name[name.length-1];
  15.                 //打印文件扩展名
  16.                 System.out.println(endname);
  17.         }
  18. }
复制代码
回复 使用道具 举报
本帖最后由 刘林虎 于 2013-4-11 11:53 编辑

1、获取已知文件的扩展名----------------------------------------------》要先读到文件,得到文件名

2、abc.txt的扩展名是txt, abc.java.txt的扩展名也是txt-------------》获取扩展名的正确性保证
  1. public class Test7 {  
  2.   
  3.     public static void main(String[] args) {  
  4.         String srcPath = "D:/java/java.copy.doc";  
  5.   
  6.         getFilenameExtension(srcPath);  
  7.     }  
  8.   
  9.     // 获取指定文件的扩展名   
  10.     public static void getFilenameExtension(String srcPath) {  
  11.         // 将源路径转换成文件对象   
  12.         File file = new File(srcPath);  
  13.   
  14.         if (file.isFile()) {  
  15.             String name = file.getName();  
  16.   
  17.             String[] exName = name.split("\\.");  
  18.   
  19.             System.out.println(exName[exName.length - 1]);  
  20.         } else {  
  21.             System.out.println("It's not a file!");  
  22.         }  
  23.     }  
  24.   
  25.     // 获取指定目录下的文件的扩展名   
  26.     public static void getDirFilenameExtension(String srcPath) {  
  27.         // 将源路径转换成目录对象   
  28.         File[] file = (new File(srcPath)).listFiles();  
  29.         for (int i = 0; i < file.length; i++) {  
  30.             if (file.isDirectory()) {  
  31.                 // 准备复制的源文件夹   
  32.                 srcPath = srcPath + "/" + file.getName();  
  33.                 getDirFilenameExtension(srcPath);  
  34.             } else {  
  35.                 // 源文件   
  36.                 File sourceFile = file;  
  37.                 // 文件名字   
  38.                 String name = sourceFile.getName();  
  39.   
  40.                 String[] exName = name.split("\\.");  
  41.   
  42.                 System.out.println(exName[exName.length - 1]);  
  43.             }  
  44.         }  
  45.     }  
  46. }  
复制代码
回复 使用道具 举报
  1. public class Test7 {
  2.         public static void main(String[] args) {
  3.                
  4.                
  5.                 File file = new File("D:/abc.java.txt");
  6.                
  7.                 // 获取文件名称
  8.                 String name = file.getName();
  9.                
  10.                 // 通过subdtring()和lastIndexOf()得到文件扩展名
  11.                 String type = name.substring(name.lastIndexOf(".") + 1);
  12.                
  13.                 System.out.println(type);
  14.         }
  15. }
复制代码
回复 使用道具 举报
李志敏 发表于 2013-4-11 12:17

谢谢,蛮简洁
回复 使用道具 举报
梁航斌 发表于 2013-4-11 11:43
我是用截取的方法,然后数组最后一个装的就是扩展名,详细代码如下

abc.java.txt同样秒杀
回复 使用道具 举报
梁航斌 发表于 2013-4-11 14:58
abc.java.txt同样秒杀

确实,你的也很犀利!谢谢
回复 使用道具 举报
梁航斌 发表于 2013-4-11 14:58
abc.java.txt同样秒杀

哥们儿,能给我具体说下split怎么用么?
回复 使用道具 举报
split在String类里面,就是你指定一个字符串来进行对原字符串来截取成字符串数组。煮个栗子:www.heima.com,我用String str[] = “www.heima.com”.split("\\.");的话,那么就能得到str[]里就有3个------{"www","heima","com"}抽象的说你指定的字符就相当于下刀点。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马