is it possible to manipulate with BufferedReader and BufferedWriter

By this I mean that we have control over BuffererReader, at what line he should stop reading and at which he should continue.

With a scanner, I could search for a specified title, and from that line to start reading values, all until to the line “end”.


public void load(String filePath) {
    boolean isVideo = false;
    String readLine;

    verifyFilePath(filePath);

    while (loadInput.hasNext()) {
        readLine = loadInput.next();
        if (readLine.equalsIgnoreCase("video")) {
            isVideo = true;
            break;
        }
    }

    while (isVideo && loadInput.hasNext()) {
        readLine = loadInput.next();
        if (readLine.equalsIgnoreCase("end")) {
            break;
        }
        String[] line = readLine.split("=");

        String key = line[0];
        String value = line[1];

        switch (key) {
            case "width":
                frameSize.setWidth(Integer.parseInt(value));
                break;
            case "height":
                frameSize.setHeight(Integer.parseInt(value));
                break;
            case "fullscreen":
                fullScreen.setFullScreen(Boolean.parseBoolean(value));
                break;
            case "fps":
                fps.setFps(Boolean.parseBoolean(value));
                break;
            default:
                System.err.println("Unknown video key: " + key);
                break;
        }
    }
    loadInput.close();
}

However, my problem is a save method. I again want to achieve same point, but this time I want to search for a value that is changed and to replace it with a new one.
I’ve looked into javadoc, and found some mark method, but didn’t work.

And what this code below does right now, iBufferedReader in the first while loop searches for “video” title, then goes all to the last line, and then on that last line he start appending this temp strings, which is not the way I want.


public void save() {
        BufferedReader bfReader;
        BufferedWriter bfWriter;
        
        try {
            bfReader = new BufferedReader(new FileReader(new File(filePath)));
            bfWriter = new BufferedWriter(new FileWriter(new File(filePath), true));

            boolean isVideo = false;
            int currentIndex = 0;
            String currentLine;
            
            while (((currentLine = bfReader.readLine()) != null) && !isVideo) {
                if (currentLine.equalsIgnoreCase("video")) {
                    bfReader.mark(currentIndex); // marks current position
                    isVideo = true;
                    break;
                }
                currentIndex++;
            }
            
            if (bfReader.markSupported()) {
                System.out.println("Mark supported... reset!");
                bfReader.reset();
            }
            
            while (isVideo && ((currentLine = bfReader.readLine()) != null)) {
                if (currentLine.equalsIgnoreCase("end")) {
                    break;
                }
                
                String[] line = currentLine.split("=");

                String key = line[0];
                String value = line[1];

                switch (key) {
                    case "width":
                        if (!value.equals(String.valueOf(frameSize.getWidth()))) {
                            // then change value
                            bfWriter.append("\t NEW WIDTH");
                        }
                        break;
                    case "height":
                        if (!value.equals(String.valueOf(frameSize.getHeight()))) {
                            // then change value
                            bfWriter.append("\t NEW HEIGHT");
                        }
                        break;
                    case "fullscreen":
                        if (!value.equals(String.valueOf(fullScreen.isFullScreen()))) {
                            // then change value
                            bfWriter.append("\t FULL SCREEN");
                        }
                        break;
                    case "fps":
                        if (!value.equals(String.valueOf(fps.isFps()))) {
                            // then change value
                            bfWriter.append("\t FPS");
                        }
                        break;
                    default:
                        System.err.println("Unknown video key: " + key);
                        break;
                }
            }
            
            bfWriter.close();
            bfReader.close();
        } catch (IOException ex) {
            System.err.println("An exception has occurred during data saving.");
            return;
        }
        System.out.println("Settings has been successfully saved!");
    }


So finally, flow control and replace value are the problems.