如何获得文件名(不要后缀)?

sAS112UUUYY111 2010-02-09 10:31:09
例如:
File f = new File("d:/d/abc.txt");

f.getName()获得的是abc.txt,如果不需要后缀,
只需要返回文件的名字怎么办呢?






...全文
1829 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
haidao8214 2010-02-11
  • 打赏
  • 举报
回复
提供2种方法:
第一种:
String fileName = "c:\\HELLO.txt";
String name = new File(fileName).getName();
name = name.substring(0, name.lastIndexOf("."));
第二种:
String fileName = "c:\\HELLO.txt";
String name = new File(fileName).getName();
String regex = "((.+)\\.)(.+)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(name);
if(m.find()) {
System.out.println(m.group(2));
}
Forrest23 2010-02-11
  • 打赏
  • 举报
回复 1


System.out.println(file.getName().split("\\.")[0]);

System.out.println(file.getName().substring(0, file.getName().lastIndexOf(".")));

Pattern p=Pattern.compile("(.*)(\\.)(\\w+$)");
Matcher matcher=p.matcher(file.getName());
if (matcher.find()) {
System.out.println(matcher.group(1));
}

Forrest23 2010-02-11
  • 打赏
  • 举报
回复

System.out.println(file.getName().split("\\.")[0]);
sd4324530 2010-02-10
  • 打赏
  • 举报
回复
1L正解,受教了!回复内容太短了
furyboo 2010-02-10
  • 打赏
  • 举报
回复

File f = new File("d:/d/abc.txt")
String fileName = f.getName();
Pattern pattern=Pattern.compile("\\\\(.+)\\.\\w+$");

Matcher matcher = pattern.matcher(fileName);

if(matcher.find())
{
  System.out.println("fileName: " + matcher.group(1));
}
呵-呵呵 2010-02-10
  • 打赏
  • 举报
回复
f.getName().split("\\.")[0]
furyboo 2010-02-10
  • 打赏
  • 举报
回复

File f = new File("d:/d/abc.ccc.txt");

这种文件名也是正常的吧
sAS112UUUYY111 2010-02-10
  • 打赏
  • 举报
回复
引用 20 楼 jypapgl 的回复:
楼上是 可一直转移?


不明白
jypapgl 2010-02-10
  • 打赏
  • 举报
回复
楼上是 可一直转移?
kasper360 2010-02-10
  • 打赏
  • 举报
回复
引用 16 楼 sas112uuuyy111 的回复:
String temp[] = loactionUrl.replaceAll("\\\\","/").split("/");
是什么意思

还是参考1楼的吧
sAS112UUUYY111 2010-02-10
  • 打赏
  • 举报
回复
还是不明白什么意思?
请教了
pjw100 2010-02-09
  • 打赏
  • 举报
回复

第一步,int a = lastIndexOf("/")
第二步,int b =lastIndexOf(".")
第三部substring(a+1,b);//截取后缀名和路径
licip 2010-02-09
  • 打赏
  • 举报
回复
是的。是要自己来把它的后缀名去掉的。
折腾的生活 2010-02-09
  • 打赏
  • 举报
回复
1L正解,过年了,回家了,csdn上不了这两周!
Qin_Tianxiang 2010-02-09
  • 打赏
  • 举报
回复
又来晚了
楼上正解
蛋黄车 2010-02-09
  • 打赏
  • 举报
回复
String test = f.getName().substring(0,f.getName().lastIndexOf("."));
darktop 2010-02-09
  • 打赏
  • 举报
回复
引用 16 楼 sas112uuuyy111 的回复:
String temp[] = loactionUrl.replaceAll("\\\\","/").split("/");
是什么意思

把\\换成/,
sAS112UUUYY111 2010-02-09
  • 打赏
  • 举报
回复
String temp[] = loactionUrl.replaceAll("\\\\","/").split("/");
是什么意思
sAS112UUUYY111 2010-02-09
  • 打赏
  • 举报
回复
引用 12 楼 opentheoo 的回复:
Java code
String fileName="";
String temp[]= loactionUrl.replaceAll("\\\\","/").split("/");if(temp.length>1){
fileName= temp[temp.length-1];
}
fileName  就是你想要的




怎么理解?
baofengyingyinaaa 2010-02-09
  • 打赏
  • 举报
回复
File file=new File("e:\\你好.txt");
System.out.println(file.getName().substring(0,file.getName().lastIndexOf(".")));
System.out.println(file.getName().split("\\.")[0]);
加载更多回复(8)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧