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

标签 java bufferedreader java-io bufferedwriter

我想复制 .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(); 
        }
}

但是程序不会删除旧文件,它只是删除该文件中的所有内容,因此该文件为空。 而且该程序不会在新文件中写入任何内容。

最佳答案

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

/* 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();
}

关于Java.io.File 没有按应有的方式删除 File,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61463840/

相关文章:

java - BufferedReader/PrintWriter 有问题吗?

java - 我可以将 Java 的 BufferedReader 与事件监听器而不是 while(true) 一起使用吗?

Java bufferedReader readline循环不会中断

java - 如何在tomcat文件夹中写入文件?

java - 将文本文件读入二维数组?

java - 带有 spring 数据的 mongodb 查询键值

java - 避免多个 if 语句的最简洁方法 - 对象创建依赖于许多参数

java - 计算 AWS4 签名后无法重置流

java - 在 Spark 外部加载 Mllib 模型

java - 比双重嵌套的 ArrayList 更有效?