gpt4 book ai didi

java - 我想删除一个文件并将新文件重命名为旧文件

转载 作者:行者123 更新时间:2023-12-02 09:38:06 26 4
gpt4 key购买 nike

如果之前有人问过这个问题,请原谅我,但我正在尝试将旧文件中的一些行写入临时文件,然后在删除旧文件之前将临时文件重命名为旧文件。下面是我尝试过的一些代码,使用通常的 File .delete() 和 .renameTo(),加上创建一个新的 public static void Delete 函数,但它总是最终创建临时文件而不删除或重命名。

        String lectid = jLabel1.getText().trim();
String passw = Lpass.getText().trim();
String name = LName.getText().trim();
String email = Lemail.getText().trim();
//trim will make sure spaces are ignored
//File temp = new File("lectemp.txt");
try {
Scanner sc = new Scanner(new File("lecturer.txt")).useDelimiter(",");
//FileWriter writer = new FileWriter("lectemp.dat", true);
FileWriter writer = new FileWriter("lectemp.txt", true);
BufferedWriter print = new BufferedWriter(writer);
sc.nextLine();
while (sc.hasNextLine()) {
String swap = sc.nextLine(); //putting it before the while statement causes it to crash
if(swap.contains(lectid + ",")){
print.newLine();
print.write(lectid + "," +name+ "," +passw+ "," +email);

}else{
print.newLine();
print.write(swap);
}
}
sc.close();
print.close();
writer.close();
File old = new File("lecturer.txt");
if(old.delete()){
System.out.println("deleted");
}else{
System.out.println("not deleted");
}
File old = new File("lecturer.txt");
File temp = new File("lectemp.txt");
old.delete();
temp.renameTo(old);
        String lectid = jLabel1.getText().trim();
String passw = Lpass.getText().trim();
String name = LName.getText().trim();
String email = Lemail.getText().trim();
//trim will make sure spaces are ignored
File old = new File("lecturer.txt");
File temp = new File("lectemp.txt");
try {
Scanner sc = new Scanner(new File("lecturer.txt")).useDelimiter(",");
//FileWriter writer = new FileWriter("lectemp.dat", true);
FileWriter writer = new FileWriter("lectemp.txt", true);
BufferedWriter print = new BufferedWriter(writer);
sc.nextLine();
while (sc.hasNextLine()) {
String swap = sc.nextLine(); //putting it before the while statement causes it to crash
if(swap.contains(lectid + ",")){
print.newLine();
print.write(lectid + "," +name+ "," +passw+ "," +email);

}else{
print.newLine();
print.write(swap);
}
}
sc.close();
print.close();
writer.close();
Deletion(old,temp);

public static void Deletion(File file1, File file2){ //temp renaming with method
if(file1.delete()){
System.out.println("File deleted");
}else{System.err.println("ERROR");}

if(file2.renameTo(file1)){
System.out.println("File renamed");
}else{System.err.println("ERROR");}
}

我希望输出是“lecturer.txt”被删除,“letemp.txt”被重命名为“lecturer.txt”但是,它总是以两者结束,没有任何内容被删除或重命名。当我尝试第一个代码时,它总是说未删除。

最佳答案

有时文件可能无法删除,例如,如果您没有文件的权限或文件仍处于打开状态。据我所知,操作系统本身不会阻止您删除该文件。

我建议您更改代码,不要使用

File.delete();

try {
Files.delete(File.deleteToPath);
} catch (Exception e)
System.out.println("Failed because: " + e.getMessage());
}

与"file"调用不同,"file"操作将抛出一个有用的异常,因此您实际上可以知道删除失败的原因。如果您的删除调用每次都失败,这应该可以轻松调试。

https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#delete(java.nio.file.Path)

关于java - 我想删除一个文件并将新文件重命名为旧文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57329395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com