文件删除和重命名问题

发布于 2024-12-20 15:14:19 字数 969 浏览 2 评论 0原文

这是我用于删除文件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹沉浮 2024-12-27 15:14:19

重命名操作是 依赖于平台,它可能会成功,也可能不会成功,不保证。

无法重命名的原因之一可能是因为已经存在一个具有该名称的文件,这意味着删除没有删除该文件,您说情况并非如此。

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 add f1.exists() and see what it returns true or false ?

回梦 2024-12-27 15:14:19

你改名两次了吗?

不应该是

System.out.println("delete----"+delete+ "rename-----"+rename);

Did you rename twice ?

Should'nt be

System.out.println("delete----"+delete+ "rename-----"+rename);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文