Java從文件路徑中獲取文件名的幾種方法


舉例:String fName =” G:\Java_Source\navigation_tigra_menu\demo1\img\lev1_arrow.gif ”

方法一:

1 File tempFile =new File( fName.trim());
2 String fileName = tempFile.getName();

方法二:

String fName = fName.trim();
String fileName = fName.subString(fName.lastIndexOf("/")+1);

方法三:

1 String fName = fName.trim();
2 
3 String temp[] = fName.split("\\\\"); /**split里面必須是正則表達式,"\\"的作用是對字符串轉義*/
4 
5 String fileName = temp[temp.length-1];

 方法四:

1 String regEx ="[^\\\\:]+?\\..*$";
2 String s = "G:lev1_arrow.gif";
3 Pattern pat = Pattern.compile(regEx);
4 Matcher mat = pat.matcher(s);
5 boolean rs = mat.find();
6 System.out.println(rs);
7 for(int i=0; i <= mat.groupCount(); i++){
8     System.out.println(mat.group(i));
9 } 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM