Home > DeveloperSection > Forums > How to write program to file Concatenation program in java i/o .
Jonas Stuart
Jonas Stuart

Total Post:11

Points:77
Posted on    July-15-2016 8:01 AM

 Java Java  Java I/O 
Ratings:


 1 Reply(s)
 196  View(s)
Rate this:
Hi buddy

Please help me to write program to file Concatenation program in java i/o.I would really appreciate your help
Thanks


Ailsa Singh
Ailsa Singh

Total Post:10

Points:70
Posted on    July-15-2016 9:10 AM

Hi Jones

Check this program code.I think it will help you.

import java.io.*;

 

public class Concatenate {

                     public static void concenateFile(String... fileName) {

                             String str = null;

                             try (BufferedWriter writer = new BufferedWriter(new FileWriter(

                                                "CombinedFile.txt"));) {

                                      for (String name : fileName) {

                                                try (BufferedReader reader =

                                                                           new BufferedReader(new FileReader(

                                                                   name));) {

                                                          while ((str = reader.readLine()) != null) {

                                                                   writer.write(str);

                                                                   writer.newLine();

                                                          }

                                                } catch (IOException e) {

                                                          System.out.println("Error reading/writing file");

                                                }

                                      }

                             } catch (Exception e) {

                                      e.printStackTrace();

                             }

                     }

 

                     public static void main(String[] args) {

                             if (args.length < 0) {

                                      System.out.println("Usage: java Concatenate file1 file2");

                                      System.exit(0);

                             }

                             concenateFile(args);

                             System.out.println("Successfully created CombinedFile.txt");

                     }

}


Modified On Jul-15-2016 09:13:25 AM

Don't want to miss updates? Please click the below button!

Follow MindStick