HOW TO WRITE PROGRAM TO FILE CONCATENATION PROGRAM IN JAVA I/O .

Jonas Stuart

Total Post:27

Points:189
Posted by  Jonas Stuart
 448  View(s)
Ratings:
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
  1. Ailsa Singh

    Post:24

    Points:168
    Re: How to write program to file Concatenation program in java i/o .

    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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!