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();

Recommended Answers

All 5 Replies

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.

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.

isn't that what i said, except with if's and not switch's?

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.