java中大小写敏感情况下怎么判断文件是否存在?在线等,急!!!

云漫观天科技 2011-03-26 11:05:20
操作系统是windows,请各位高手看清标题,重点是要求大小写敏感!!!!!!!!!!!!
例如:
String s = "a.txt";
File file = new File(s);
boolean bool = file.exsits();

查找的文件是a.txt,但系统中的实际文件名是A.txt,目前bool还是会返回true,可我希望他返回false!!!

请问各位高手,应如何解决呢?谢谢!
...全文
384 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
shine333 2011-03-28
  • 打赏
  • 举报
回复
File.getCanonicalPath()可以返回Windows中正确大小写的文件名,不过是绝对路径,File.getCanonicalFile().getName()只返回文件名。然后你和你原始的文件名判断即可。
比如

File file= new File("a.txt");
System.out.println(dir.getCanonicalPath()); // X:\dir\A.txt
System.out.println(dir.getCanonicalFile().getName()); // A.txt
liuhanpo1984 2011-03-28
  • 打赏
  • 举报
回复
看他自己解决问题的方式推测,他好像问的是:是不是字符串(文件名)大小写敏感?
hejun19860620 2011-03-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bao110908 的回复:]
在 Windows 下做这种判断没有意义,Windows 下的路径名就不区分大小写。
[/Quote]

好像是哟,我查文件的时候,按A和a都能找出来!
  • 打赏
  • 举报
回复
在 Windows 下做这种判断没有意义,Windows 下的路径名就不区分大小写。
云漫观天科技 2011-03-26
  • 打赏
  • 举报
回复
谢谢大家,自己找到方法了:
File file=new File("文件名");
String name=file.getCanonicalFile().getName();
if(name.equals("大小写敏感的文件名")){
//TODO:逻辑
}
zqfddqr 2011-03-26
  • 打赏
  • 举报
回复
boolean bool = file.exsits()||file.getFileName().indexOf("A.txt");

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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