4 Replies - 1064 Views - Last Post: 30 September 2013 - 06:02 AM Rate Topic: -----

#1 nellykvist   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 18-September 13

is it possible to manipulate with BufferedReader and BufferedWriter

Posted 30 September 2013 - 04:18 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: is it possible to manipulate with BufferedReader and BufferedWriter

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: is it possible to manipulate with BufferedReader and BufferedWriter

Posted 30 September 2013 - 05:23 AM

If you used a standards-compliant property file, you wouldn't have to jump through those hoops. Why not just read and write regular properties?
Was This Post Helpful? 0
  • +
  • -

#3 nellykvist   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 18-September 13

Re: is it possible to manipulate with BufferedReader and BufferedWriter

Posted 30 September 2013 - 05:30 AM

http://technojeeves..../74-string-list
You mean to work with this util class ? I think now I got your point. I did not understand you well yesterday..
I will try that out.
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: is it possible to manipulate with BufferedReader and BufferedWriter

Posted 30 September 2013 - 05:42 AM

No, that's a different point, though it would even make your current operations easier.

I'm talking about using Properties in the normal way
Was This Post Helpful? 1
  • +
  • -

#5 nellykvist   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 18-September 13

Re: is it possible to manipulate with BufferedReader and BufferedWriter

Posted 30 September 2013 - 06:02 AM

However, I've just made changes to my settings class with this util class StringList. Works fine.
About Properties class, I will try that out in next project or later in this, when I write better code.

This post has been edited by nellykvist: 30 September 2013 - 06:02 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1