美文网首页JavaFX
Java通过路径(地址)获取文件名的三种方式

Java通过路径(地址)获取文件名的三种方式

作者: 洞链 | 来源:发表于2019-10-12 15:36 被阅读0次
package test;  
  
import java.io.File;  
  
public class FileName {  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
//      举例:  
        String fName =" G:\\Java_Source\\navigation_tigra_menu\\demo1\\img\\lev1_arrow.gif ";  
  
//      方法一:  
  
        File tempFile =new File( fName.trim());  
  
        String fileName = tempFile.getName();  
          
        System.out.println("fileName = " + fileName);  
  
//      方法二:  
  
        String fName = fName.trim();  
  
        String fileName = fName.substring(fName.lastIndexOf("/")+1);  
        //或者  
        String fileName = fName.substring(fName.lastIndexOf("\\")+1);  
          
        System.out.println("fileName = " + fileName);  
  
//      方法三:  
  
        String fName = fName.trim();  
  
        String temp[] = fName.split("\\\\"); /**split里面必须是正则表达式,"\\"的作用是对字符串转义*/  
  
        String fileName = temp[temp.length-1];  
          
        System.out.println("fileName = " + fileName);  
  
    }  
  
}

相关文章

网友评论

    本文标题:Java通过路径(地址)获取文件名的三种方式

    本文链接:https://www.haomeiwen.com/subject/wvwqmctx.html