java截取路径字符串--得文件名
E:\AndroidProgram\java\MyApplication\good.mp3得到字符串"good.mp3"...
E:\Android Program\java\MyApplication\good.mp3
得到字符串 "good.mp3" 展开
得到字符串 "good.mp3" 展开
3个回答
展开全部
// 举例:
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);
// 方法二:
fName = fName.trim();
// fileName = fName.substring(fName.lastIndexOf("/")+1);
// 或者
fileName = fName.substring(fName.lastIndexOf("\\")+1);
System.out.println("方法二:fileName = " + fileName);
// 方法三:
fName = fName.trim();
String temp[] = fName.split("\\\\"); /**split里面必须是正则表达式,"\\"的作用是对字符串转义*/
//temp[] = [G:, Java_Source, navigation_tigra_menu, demo1, img, lev1_arrow.gif]
System.out.println("temp[] = " + Arrays.toString(temp));
fileName = temp[temp.length-1];
System.out.println("方法三:fileName = " + fileName);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询