I am using the following code to write to the file out1.txt:
try{
FileWriter fostream = new FileWriter("out1.txt");
BufferedWriter out = new BufferedWriter(fostream);
out.write("jlsdfjdlsfhsdkjf");
out.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
Now i want to write to multiple files in the same code (let us say 2 files).
Do i need to define a new object other than fostream to write to another file out2.txt or i could use the same object to write to out2.txt after i close it once?
I will try to explain it through a code perhaps:
try{
FileWriter fostream = new FileWriter("out1.txt");
BufferedWriter out = new BufferedWriter(fostream);
out.write("dffjdlfjdklfjdklfdj");
out.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
try{
FileWriter fostream = new FileWriter("out2.txt");
BufferedWriter out = new BufferedWriter(fostream);
out.write("gfgdgfdfg");
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}