Hi all,
how can I write lines that I read from multiple files to one destination file in java??
the method write (FileWriter) can do this
When I read multiple files and save these line in string called for example "line" ..how can write line on the distination file? I want an helpful example for writing on file to understand that because I search on google but I don't get what I want ....
The method write that I used ...write only short string , and I want to copy contents of multiple files on one file....

In my code , i read 4 files in directory (filename) ,depending on there pathes ...
but , the error in write to other file ! .... the output file is empty! i want to copy what i read to ouhtput file ? whats the error?...

public class  newtestbuffer{
      public static String line;
      public static File f = null;
      public static File[] paths;
      public static String fi;

 public static void main(String[]ss) throws IOException {

         f = new File("C:\\filename\\src\\filename");

         // returns pathnames for files and directory
         paths = f.listFiles();

         // for each pathname in pathname array
         for(File path:paths)

            // prints file and directory paths
           // System.out.println(path);

           FileReader fr = new FileReader((fi));

            BufferedReader br = new BufferedReader(fr);

             line = br.readLine();

            while (line != null) {

            line = br.readLine(); 

        catch (IOException e) {
        }}catch(Exception e){
         // if any error occurs

////////////writ on output file///
           File file = new File("C:\\cyclnew.java");
            FileWriter fileWriter = new FileWriter(file);
            //fileWriter.write("This is ");


        }catch(Exception e){
         // if any error occurs



First that's poorly formatted code.

Second it looks like you read the last line from the file in lines 32 to 38. Let's call it a bad implementation. Try again. Some folk want others to write their code but it's best you see what's off and rewrite it.

No, I read the lines of all files (4 files ) and print them on screen by the for of path and bufferedreader....

But I don't know how to write on output file...

Yes you read and printed but then that loop ended with that one last line in the string variable. If you need to go back to basics in coding, do that but your code as-is is very borked.

You know how to write one line (its in your code at line 59), so all you need to do is to place that inside your loop(s) so that its executed for each line that you read from the input files.

Yes ..Mr. rproffitt ..when for loop ended ..print only last line... I place it inside loop to write all lines but also , it doesn't print all line!..

Yes..Mr. JamesCherrill, I know how to write but the error is print one line and previously I plased it inside loop but also I get same result ?
I don't know what's the error ?
I will try it again ... thank you for your help. ..

Thanks all for help...

Yes, keep trying! And remember, if you need help you should ALWAYS post the relevant code.

I try it Mr. James ..and plased it in loop but the output file is empty ..it doesn't copy any content ? What should I do?...

Read my last post.

Ok..Mr. James... I try it again and copy contents and the error in my loop I solve it ... thank you very much for your help .... it's really a good and helpful website...

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.