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

Recommended Answers

All 8 Replies

You are confusing variables and objects. Variables just contain references (pointers) to an object. You create new FileWriter and BufferedWriter objects each time. Because you only use one at a time it's perfectly OK to re-use the same variables to refer to those objects.

why don't you just do:

FileWriter foStream;

try{
    foStream = new FileWriter("file1.txt");
    ....
}
catch(Exception e){

}

try{
  foStream = new FileWriter("file2.txt");
  ...
}
catch(Exception e){

}

you can also put them in the same try-block.

Yes, if we're discussing the try/catch, then with Java 7 or later you should be using a "try with reources" as being by far the easiest and safest way to ensure your streams are closed correctly under any/all circumstances, including when an exception is thrown. (And if you're not using Java 7 then you should be!)

try ( FileWriter foStream = new FileWriter("file1.txt");
      BufferedWriter out = new BufferedWriter(fostream)
) {
   out.write("gfgdgfdfg"); 
}
catch(Exception e){
   e.printStackTrace();
}

http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

I have a parent file containig data alongwith dates. I am trying to sort my data into 12 files, one for each month. Therefore i am trying to create 12 output stream objects for each file. I will read the data from the parent file and will keep on writing the data in the corresponding file using the given output stream object for that file. My code says: '.class' expected in line where i am calling the crtoutobj function (in the for loop).

import java.io.*;
import java.lang.*;
import java.text.*;
import java.util.*;

public class cde{
    public static void main(String args[]){
     int k;
String filestr;
FileWriter fostream[] = new FileWriter[12];
BufferedWriter out[] = new BufferedWriter[12]; 

                    for(k=1;k<=12;k++){
            filestr="out"+k+".txt";
            crtoutobj(filestr, k, fostream[], out[]);
            }   
}
public static void crtoutobj(String s, int l, FileWriter gostream[],BufferedWriter rut[]){
try{
                     gostream[l] = new FileWriter(s);
                     rut[l] = new BufferedWriter(fostream[l]);          
                     System.out.println("Output stream for object"+" l"+" created");                    

                }catch (Exception e){
                    System.out.println("Error in creating output stream for object "+l);
                }  
}        
}

Could you please help me out with this error? Where am i going wrong?

@stultuske: I can use the multiple try blocks but i just wanted to make my program shorter by using a method like in my above post. This is always an option if nothing works out. :D

When you pass an array as a parameter you just use its name, you do not append a []

Oh. Ok. I am a newbie so please don't mind asking these kinds of doubts. :)

Most of the people posting in this forum are newbies, so please don't worry about asking questions!
JC

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.