0

I am writing a program based on file. My code looks something like this,

   public class clazz {
    void somemethod() {
        StringBuffer s = new StringBuffer();
        // some code at final
        write(buf);
    }

    void write(StringBuffer buffer) {
        FileWriter f = new FileWriter(fileName);
        BufferedWriter b = new BufferedWriter(f);
        // implemented write, flush, close
    }
}

when I call somemethod method, the file gets open every time. I need to open the file once using constructor and reuse the BufferedWriter. Help me regarding this.

1 Answer 1

2

It looks like something that your adviser told you to do and you have no clue.

  1. Make BufferedWriter a instance variable
  2. Initialize it in your constructor.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.