How to write program to file Concatenation program in java i/o .

Total Post:27

Points:189
 766  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. 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 Apr-11-2018 04:51:43 AM

Answer