文件删除和重命名问题
这是我用于删除文件 f1 并将文件 f2 重命名为 f1 的代码。但它返回 false。
String strLine;
File f1 =new File("C:\\Equinox\\RootSipResource\\root\\root.properties");
File f2 =new File("C:\\Equinox\\RootSipResource\\root\\root1.properties");
FileInputStream fin = new FileInputStream(f1);
BufferedReader br = new BufferedReader(new InputStreamReader(fin,"UTF-8"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(f2), "UTF-8");
while ((strLine = br.readLine()) != null) {
strLine = strLine.replace("root.label.43.2=PBS Kids"," root.label.43.2=PBS Kids NEW");
out.write(strLine);
out.write("\n");
}
out.flush();
out.close();
br.close();
//fin.close();
boolean delete= f1.delete();
boolean rename=f2.renameTo(f1);
System.out.println("delete----"+delete+ "rename-----"+rename);
Here is the code iam using for deleting file f1 and renaming a file f2 to f1..But it returns false.
String strLine;
File f1 =new File("C:\\Equinox\\RootSipResource\\root\\root.properties");
File f2 =new File("C:\\Equinox\\RootSipResource\\root\\root1.properties");
FileInputStream fin = new FileInputStream(f1);
BufferedReader br = new BufferedReader(new InputStreamReader(fin,"UTF-8"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(f2), "UTF-8");
while ((strLine = br.readLine()) != null) {
strLine = strLine.replace("root.label.43.2=PBS Kids"," root.label.43.2=PBS Kids NEW");
out.write(strLine);
out.write("\n");
}
out.flush();
out.close();
br.close();
//fin.close();
boolean delete= f1.delete();
boolean rename=f2.renameTo(f1);
System.out.println("delete----"+delete+ "rename-----"+rename);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
重命名操作是 依赖于平台,它可能会成功,也可能不会成功,不保证。
无法重命名的原因之一可能是因为已经存在一个具有该名称的文件,这意味着删除没有删除该文件,您说情况并非如此。
在
f2.renameTo(f1)
之前,您可以添加f1.exists()
并查看它返回 true 或 false 吗?Rename operation is platform dependent, it may or may not succeed, no guarantee.
One reason why this is not able to rename may be because there is already a file with the name, which means delete didn't delete the file, which you say isnt the case.
Before
f2.renameTo(f1)
can you addf1.exists()
and see what it returns true or false ?你改名两次了吗?
不应该是
Did you rename twice ?
Should'nt be