【问题标题】:(java) How to update BufferedReader after making changes to the file(java) 更改文件后如何更新 BufferedReader
【发布时间】:2020-11-14 16:31:21
【问题描述】:

我目前正在为大学任务制作一个简单的文本编辑器,并通过用户输入,在现有文件上进行基本操作(切换行位置或单词位置)。

我的程序由一个包含所有逻辑的 FileManipulator 类和一个 GUI 类(main 所在的地方)组成

public class FileManipulator {

    File file;//current file
    BufferedReader fileReader; //Reader for the file
    String currentDirectory; //Always verified due to setDirectory() validation

    public FileManipulator() throws IOException{ ...;}

    public void loadFile() throws IOException{
        if(this.fileReader != null) {
            this.fileReader.close();
        }
        this.file = new File(this.currentDirectory);

        //if there isn't a txt file, ask user if he wants to create one
        if(!file.exists()) { //logic for creating a new file}

        //After we are sure a file exists in this directory, we init the fileReader
        this.fileReader = new BufferedReader(new FileReader(this.file));
        this.fileReader.mark(READ_AHEAD_LIMIT);
    }

    public void switchLines(int line1, int line2) throws ArrayIndexOutOfBoundsException, IOException {
        //Logic about switching the lines here

        //Writing to the file
        writeToFile(fileContents);
        
    }

    //Will open a writer, write to the file and close the writer
    private void writeToFile(ArrayList<String> listToPrint) throws IOException{
        StringBuilder tempList = new StringBuilder();
        for (String s : listToPrint) {
            tempList.append(s);
            tempList.append('\n');
        }

        FileWriter fileWriter = new FileWriter(this.file);
        fileWriter.append(tempList);
        fileWriter.close();
        /* In this function, we make changes to the file, however, when using getFileText()
         * the changes written in the file aren't noticed and the old file is returned
         */
    }

   //Problematic function
   public String getFileText() throws IOException{
        fileReader.reset();
        StringBuilder finalText = new StringBuilder();

        String temp;

        while((temp = fileReader.readLine()) != null) {
            finalText.append(temp);
            finalText.append('\n');
        }

        return finalText.toString();
    }

对文件进行更改并保存后,我的 BufferedReader 没有更新。它仍然会读取更改前的内容。

当我使用 loadFile 方法并重新加载同一个文件时,缓冲阅读器会关闭并重新打开,因此文件的内容会更新,但是每次我使用缓冲阅读器时打开和关闭它并不是最优雅的解决方案。

我也想过拥有一个文件内容的 ArrayList 并仅在关闭程序时更新它,但如果我错过了一个简单的修复,那将是一个不必要的练习。

【问题讨论】:

  • 阅读文件后确保关闭它。您可能无法在文件仍处于打开状态时对其进行写入。
  • BufferedReader 值得注意的部分是 Buffered 部分。重新设置并再次读取时,您正在读取 缓冲区,而不是文件。您必须关闭并重新打开才能再次读取文件。

标签: java file bufferedreader


【解决方案1】:

BufferedReader 就是这样工作的。 resetmark 完全全部在内存中完成。

有很多解决方案:

  1. 嘿,无论如何,您已经承诺将整个内容存储在内存中。为什么不将您的文本文件表示为BufferedReader 而不是StringList&lt;String&gt; 或诸如此类,并更新更新代码,然后将其写入磁盘,并且除了应用程序时没有读取代码开始?为什么要使用磁盘作为中间件?

  2. 对文件使用 FileChannel、RandomAccessFile 或其他一些抽象,它们可以更好地支持实际从磁盘读取。请注意,您也需要此 on-write:在许多操作系统上,“从头开始将所有内容写入此文件”实际上会创建一个新文件,并且任何打开的文件句柄仍指向无法从文件系统访问的旧文件.

  3. 只需..关闭该文件读取器并再次打开它,甚至设置一个布尔标志以让读取代码知道写入代码进行了更改,并且仅在标志为“真”时才这样做(然后将其设置为当然是假的)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    相关资源
    最近更新 更多