0

This is my first post and I am new to this so bear with me...I am trying to write a program that will output messages to 1 or more text files. For example if I want to write to 3 log files, depending on what is happening in the code. I may want to write to one but not the other. How do I code it for more than one file and how do I tell it which one to write to. I know how to do it for one file... how do I do it for more than one? This is what I have for one..

PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter("firstlog.txt", true)));

out.print("this is a message to first log.txt");
out.close();
3
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by Jaulm
0
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter("firstlog.txt", true)));

out.print("this is a message to first log.txt");
out.close();

well, you would use a(n)( series of) if statement(s) and put your print statements in it/them,
all you really need to write to more than one file is more than one PrintWriter, with different file names in them.

0

Umm, Have you thought about creating separate PrintWriter objects? Define a rule collection and use switch case to check which rules match. Accordingly call the PrintWriter object's print method associated with the respective log files.

0

Yeah your right, =_= I was too lazy to read your post. Sorry.

But Just so you know, having a switch case here is less prone to error. But still whichever you use, in the end, how you program it will matter.

0

Thank you so much for the feedback I will try that!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.