0

I have this code to write to a file when I add a user to an array list. The code works fine:

public void writeToFile(String content)
    {
        try {
            File file = new File("H:/JavaWorkspace/TradingPlatformProject/User_Report.txt");

            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content + "\n" );

            bw.close();
            logger.info("Recorded to User Activity file");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

I want to write to a separate file when a user does something differently (say, request a permission upgrade). Is there any way I can write to a new file "UserRequests.txt" without duplicating this code?

1
  • Perhaps simply add in the file name or operation as a parameter? Commented Jun 14, 2013 at 15:05

3 Answers 3

7

Why not make the method more general?

  public void writeToFile(String content, String fileName, String path)
  {
        try {
            File file = new File(path + fileName);

            if (!file.exists()) {
                file.createNewFile();
            }
            FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content + "\n" );

            bw.close();
            logger.info("Recorded to User Activity file");
        } catch (IOException e) {
            e.printStackTrace();
        }
  }

Then you could use the method for writing all kinds of files :3

Sign up to request clarification or add additional context in comments.

Comments

1

You should probably just use a 2nd argument, as in the following. Moreover, you should close your Writers in a finally block. That way, you would be sure that the Writers are closed even if a exception occurred while writing.

public void writeToFile(String content, String path)
{
    FileWriter fw
    BufferedWriter bw 
    try {
        File file = new File(path);

        if (!file.exists()) {
            file.createNewFile();
        }
        fw = new FileWriter(file.getAbsoluteFile(), true);
        bw = new BufferedWriter(fw);
        bw.write(content + "\n" );

        bw.close();
        logger.info("Recorded to User Activity file");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        bw.close();
        fw.close();
    }
}

Comments

0

I would just pass in the file you want to write to:

public void writeToFile(String content, String filename){

And then:

File file = new File("H:/JavaWorkspace/TradingPlatformProject/"+filename);

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.