3种Java从文件路径中获取文件名的方法

来源:互联网 发布:js调用手写输入法 编辑:程序博客网 时间:2024/05/18 10:00
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);}}
文章转自:http://rogerfederer.iteye.com/blog/1039666
阅读全文
1 0
原创粉丝点击