new File() 新建文件路径异常

九零大叔芭蕉 2018-07-22 03:26:51
在spring boot 项目中写了个压缩文件的工具类,使用的是zip4j。可是在我新建文件时路径一直不对。

File file1=new File("‪C:\\Users\\Administrator\\Desktop\\world_cup.sql");
File file2=new File("C:\\Users\\Administrator\\Desktop\\worldCup_ERD.xlsm");


然后控制台的错误信息时:
net.lingala.zip4j.exception.ZipException: net.lingala.zip4j.exception.ZipException: file does not exist: D:\WorkSpace\worldcup\‪C:\Users\Administrator\Desktop\world_cup.sql
Caused by: net.lingala.zip4j.exception.ZipException: file does not exist:

D:\WorkSpace\worldcup\‪C:\Users\Administrator\Desktop\world_cup.sql
这个路径怎么就自动改变了呢?
worldcup是我的spring boot 项目名。
...全文
1371 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingmax54212008 2018-07-23
  • 打赏
  • 举报
回复
具体的debug设置一下断点。
从可以运行的case开始剖析!
stacksoverflow 2018-07-23
  • 打赏
  • 举报
回复
不用跑,贴到记事本里就看出来了,前面多了个特殊字符,所以被认为相对路径了
String str1="C:/Users/Administrator/Desktop/world_cup.sql";//file的路径
String str2="* C:/Users/Administrator/Desktop/world_cup.sql";//file1的路径
youyiyang 2018-07-22
  • 打赏
  • 举报
回复
原因可能应该是Spring MVC 服务器在你控制器修改后会自己重新热启动配置 一下,所以你有时就先把服务器关闭了,再启动一下,甚至可以重新启动电脑一下
九零大叔芭蕉 2018-07-22
  • 打赏
  • 举报
回复
找到问题了,但是原因你们一定找不出来为什么。我把代码贴出来,你们可以自己试试。我到现在都不知道为什么
File file=new File("C:/Users/Administrator/Desktop/world_cup.sql");
if(!file.exists()){
System.out.println("=====================");
}
File file1=new File("‪C:/Users/Administrator/Desktop/world_cup.sql");
if(!file1.exists()){
System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
}


String str1="C:/Users/Administrator/Desktop/world_cup.sql";//file的路径
String str2="‪C:/Users/Administrator/Desktop/world_cup.sql";//file1的路径

System.out.println(str1.length()+"\n"+str2.length());

if(str1.equals(str2)){
System.out.println("1111111111111");
}else{
System.out.println("22222222222222");
}


主要是路径的问题,看一下这两个字符串为什么会不同。
九零大叔芭蕉 2018-07-22
  • 打赏
  • 举报
回复

我又新建了一个文件,然后file就可以,file1就出错了,这是为什么啊
九零大叔芭蕉 2018-07-22
  • 打赏
  • 举报
回复
引用 2 楼 youyiyang 的回复:
你把\\换成/试一下

还是不行
youyiyang 2018-07-22
  • 打赏
  • 举报
回复
你把\\换成/试一下
九零大叔芭蕉 2018-07-22
  • 打赏
  • 举报
回复
很懵逼,这是为什么啊?

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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