关于File.separator问题

budetcbc007 2010-06-09 12:50:10
File.separator用来获取分隔符

在windows中,如果指定一个变量的内容为
filePath=D:\ProgramTool\apache\tomcat-6.0.18\webapps\offic/mdel/ted

那么filePath.lastIndexOf(File.separator) 将是最后一个\ (offic 前面的),而不是(ted前面的/)

请问如何获取最后一个/

我想获取最后一个分隔符/.
难道要把filePath的/全部转为\????
...全文
369 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
宁静-夏天 2010-06-09
  • 打赏
  • 举报
回复
java的File对象支持 File.separator 自动的转化。

在getAbsolutePath() 可以转化为一致的System的文件分割。

你再用 File.separator 来区分好了。免得你自己根据操作系统的变化转来转去。
「已注销」 2010-06-09
  • 打赏
  • 举报
回复
new File(filePath).getParent()
dr_lou 2010-06-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 goldenfish1919 的回复:]
换一下怎么了?费劲吗?
filePath=D:\ProgramTool\apache\tomcat-6.0.18\webapps\offic/mdel/ted
String temp=filePath.replace("/","\\");
temp.lastIndexOf(File.separator)
[/Quote]

换一下怎么了?费劲吗?
若鱼1919 2010-06-09
  • 打赏
  • 举报
回复
换一下怎么了?费劲吗?
filePath=D:\ProgramTool\apache\tomcat-6.0.18\webapps\offic/mdel/ted
String temp=filePath.replace("/","\\");
temp.lastIndexOf(File.separator)
dddddz 2010-06-09
  • 打赏
  • 举报
回复
String fp= file.getAbsolutePath();
String[] token = fp.split("/");
String fn = token[token.length-1];

62,620

社区成员

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

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