关于File中使用file.renameTo("path")和file.delete()失败的原因

下面的代码的主要功能就是删除将原来写入文件的welcome to www.sofencyandalice.top中的andalice删除,因为不知道如何在原文件中删除更新,索性先读取存储到字符串中,然后删除文件,再创建文件最后写入新文件中(大佬莫笑),最后更改下文件名

  1. 在这个过程中就同时遇到了这两个糟糕的情况,刚开始没有写 System.out.println(file.exists());进行判断,自己认为已经删除了,但是后来测试时发现输出结果竟然为true,主要原因就是在Scanner 进行读取文件流时未关闭即未写change.close()这样是无法进行删除的因为Scanner进程依旧在使用文件。
  2. 当写上change.close()时,在控制台输出的
       System.out.println(file.delete());//显示false
       System.out.println(file.exists());//false
       System.out.println(file);//依旧显示要删除文件的路径
    

``

  1. 第一句和第二句本身就是矛盾的这是为神魔 我咨询老师后,老时的原话是

首先,显示原路径不等于删除失败。File封装的就是一个文件路径,不存的文件也可以得到file对象。其次,真的删除失败有很多可能,比如只读文件或者系统文件都删不掉

反应一段时间后恍然大悟,只需要确定file.exists()是false就了可以判断文件已经删除了。

  1. 重命名失败的主要原因也是如此,读取的文件流或其他进程在占据着文件所以导致无法进行更改名字.

可以仔细看下下面的代码如何实现的,从案例中总结出自己的理解这样理解更深刻

public class deleteStringInFile {
    public static void main(String[] args) throws IOException{
        //首先创建文件并且在文件中写入数据
        File file=new File("D:\\test.txt");
        PrintWriter input=new PrintWriter("D:\\test.txt");
        input.print("welcome to www.sofencyandalice.top");
        input.close();//存储到文件中

        Scanner change=new Scanner(file);
        String str=change.nextLine();
        String str2=str.replaceAll("andalice","");
        System.out.println(str2);

        change.close();//读取文件流关闭

        file.delete();//先删除再创建 效率低暂时未找到其他办法进行更新D:\\poem
        System.out.println(file.delete());//删除失败
        System.out.println(file.exists());
        System.out.println(file);

        File file1=new File("D:\\test.txt");
        file1.createNewFile();
        System.out.println(file1.exists());

        PrintWriter input1=new PrintWriter("D:\\test.txt");
        input1.print(str2);
        input1.close();

        //改名
        boolean st=file1.renameTo(new File("D:\\Test.txt"));
        System.out.println(st);


        System.out.println(System.getProperty("user.dir"));//显示文件的
    }
}
```java import java.io.File; import java.io.FileWriter; import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; import java.io.FileNotFoundException; public class FileOperations { public static void main(String[] args) { createFile(); writeFile(); readFile(); copyFile(); moveFile(); deleteFile(); renameFile(); } public static void createFile() { try { File file = new File("filename.txt"); if (file.createNewFile()) { System.out.println("File created: " + file.getName()); } else { System.out.println("File already exists."); } } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } public static void writeFile() { try { FileWriter writer = new FileWriter("filename.txt"); writer.write("Hello, World!"); writer.close(); System.out.println("Successfully wrote to the file."); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } public static void readFile() { try { BufferedReader reader = new BufferedReader(new FileReader("filename.txt")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (FileNotFoundException e) { System.out.println("File not found."); e.printStackTrace(); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } public static void copyFile() { File srcFile = new File("path/to/original_file.txt"); File destFile = new File("path/to/new_file.txt"); try { Files.copy(srcFile.toPath(), destFile.toPath()); System.out.println("File copied successfully."); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } public static void moveFile() { File srcFile = new File("path/to/original_file.txt"); File destDir = new File("path/to/destination_directory/"); try { Files.move(srcFile.toPath(), destDir.toPath().resolve(srcFile.getName())); System.out.println("File moved successfully."); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } public static void deleteFile() { File file = new File("path/to/file.txt"); if (file.delete()) { System.out.println("File deleted: " + file.getName()); } else { System.out.println("Failed to delete the file."); } } public static void renameFile() { File srcFile = new File("path/to/original_file.txt"); File destFile = new File("path/to/new_file.txt"); if (srcFile.renameTo(destFile)) { System.out.println("File renamed successfully."); } else { System.out.println("Failed to rename the file."); } } } ``` 注意:在Java,文件复制、移动和删除操作需要使用Java的`java.nio.file.Files`类。所以,请确保在代码导入了`java.nio.file.Files`类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值