Java.io.File 没有按应有的方式删除文件

Java.io.File doesn't delete File as it should

我想复制 .txt 文件中的文本但没有最后一行。 所以我所做的是,我在 BufferedReader 的帮助下阅读了整个文件(接受最后一行)。 比我删除旧文件并创建新文件。 比我尝试在新文件中写入复制的文本。

public class BufferedIO {
    public BufferedWriter bufWriter;
    public BufferedReader bufReader; 
    private StringBuilder inhalt;
    private File file; 
    private String inhaltString; 
    private int anzZeichen; 

    public BufferedIO(String Speicheradresse) {

        try {

            file = new File(Speicheradresse); //file object gets initialized with already existing File "Stundenzettel.txt"

            bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true))); 

            bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

            // the next 8 are there to count how many Characters are inside the File "Stundenzettel.txt" 
            inhalt = new StringBuilder(""); 
            inhaltString = bufReader.readLine();

            bufReader.mark(anzZeichen);

            while(inhaltString != null) {
                inhalt.append(inhaltString).append("\n");
                inhaltString = bufReader.readLine(); 
            }

            anzZeichen = inhalt.length(); 

            }
            catch(IOException exc) {
                System.out.println("IOException... Well.. That might be a problem."); 
            }

    }
    //function where the problem is situated 
    public void deleteLastInput() throws IOException {

        bufReader.close();
        bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); 

        StringBuilder inhaltKopie = new StringBuilder(""); 
        String newInhalt; 
        //everyLine has 150 character, so as soon as there aren't more than 150 character left, the Reader stops to read the File. 
        while(anzZeichen > 150) { 
            inhaltKopie.append(bufReader.readLine()).append("\n");
            anzZeichen -= 150; 
        }
        //String newInhalt is initialized with the copied Text 
        newInhalt = inhaltKopie.toString(); 

        //right here I close the bufferedReader and the BufferedWriter so that I can delete the file
        bufReader.close();
        bufWriter.close();

        //old file gets deleted 
        file.delete(); 

        //creating the new File
        file.createNewFile(); 

        //initializing the BufferedWriter + bufferedReader to the new File
        bufReader = new BufferedReader(new InputStreamReader(new FileInputStream("Resources/Stundenzettel.txt"))); 
        bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Resources/Stundenzettel", true))); 

        //bufWriter writes the copied Text inside the new File
        bufWriter.write(newInhalt);

        //not so important for this problem:  
        anzZeichen = newInhalt.length(); 
        }
}

但是程序并没有删除旧文件,它只是删除了这个文件里面的所有东西,所以这个文件是空的。 而且程序也没有在新文件中写入任何内容。

您必须指定整个文件路径。不仅 "irgendwas.txt" 而且 "c:/..."

您必须指定整个文件路径。而不是 "something.txt" 类似 "c:/..."

您需要在 deleteLastInput() 方法上使用相同的文件

bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));

尝试在 bufWriter.write(newInhalt) 之后使用 bufWriter.flush()。 请注意 delete() returns 文件是否被删除的状态。可能程序没有删除文件的适当权限

Files.delete(Paths.get(your_file_name)); //should work

以下代码将加载文件的内容,删除最后一行并将修改后的内容写回同一文件。结果是原始文件,但没有最后一行。

/* Required imports.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
 */
Path path = Paths.get("path", "to", "your", "file");
try {
    List<String> lines = Files.readAllLines(path);
    int count = lines.size();
    if (count > 0) {
        String removed = lines.remove(count - 1);
    }
    Files.write(path, lines, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
}
catch (IOException xIo) {
    xIo.printStackTrace();
}