1 2 Previous Next 17 Replies Latest reply: Nov 9, 2007 10:34 AM by 807603 RSS

    How to merge txt files

    807603
      Hey!
      I would like you to give me a hint
      I have the some files with data inside each file like this

      filename ---------------->data
      a.txt --------------------> my name is /n John
      b.txt --------------------> Your name is /n Maria
      c.txt --------------------> His name is /n Tony
      d.txt --------------------> Her name is /n Alexandra

      ,where /n denotes a new line


      Do you have any idea how could I merge them in a single txt file ,this is

      e.txt --------------------->my name is /n John /n Your name is /n Maria /n His name is /n Tony /n Her name is/n Alexandra

      Thanks in advance!
        • 1. Re: How to merge txt files
          807603
          (1)Open each file in succession
          (2)Read each file in, add to a String
          (3)Output that String to e.txt
          • 2. Re: How to merge txt files
            807603
            read from each text file

            every time you read a line, put it into a StringBuilder

            when you read all your files, write out the toString() of the builder to a new file.
            • 3. Re: How to merge txt files
              807603
              Thanks for the immediate replies
              Is there any sample code anywhere for that? Or anything similar so that i can improvise from that.
              • 4. Re: How to merge txt files
                807603
                Try searching Google for "java file io" and you'll probably get innumerable examples.
                • 5. Re: How to merge txt files
                  masijade
                  practissum wrote:
                  Try searching Google for "java file io" and you'll probably get innumerable examples.
                  Oh, their numerable, otherwise Google couldn't say "1-10 of 11234523" ;-)
                  • 6. Re: How to merge txt files
                    807603
                    Thanks again.
                    Sorry for posting twice the same thread ,but during writing i have mistakenly pressed the post button without knowing that i could edit anything afterwards ,so i made a new thread .Then i saw what i did and tried to delete one of the threads....but couldn't!
                    • 7. Re: How to merge txt files
                      807603
                      masijade. wrote:
                      practissum wrote:
                      Try searching Google for "java file io" and you'll probably get innumerable examples.
                      Oh, their numerable, otherwise Google couldn't say "1-10 of 11234523" ;-)
                      Oh fine, fine. Anything to prove me wrong... :^p
                      • 8. Re: How to merge txt files
                        807603
                        csfyrakis wrote:
                        Thanks again.
                        Sorry for posting twice the same thread ,but during writing i have mistakenly pressed the post button without knowing that i could edit anything afterwards ,so i made a new thread .Then i saw what i did and tried to delete one of the threads....but couldn't!
                        It's okay; it happens. Just be careful in the future!
                        • 9. Re: How to merge txt files
                          807603
                          masijade. wrote:
                          practissum wrote:
                          Try searching Google for "java file io" and you'll probably get innumerable examples.
                          Oh, their numerable, otherwise Google couldn't say "1-10 of 11234523" ;-)
                          As a programmer I just use one, two, three, allot so in my view it is 'innumerable' .
                          • 10. Re: How to merge txt files
                            masijade
                            sabre150 wrote:
                            masijade. wrote:
                            practissum wrote:
                            Try searching Google for "java file io" and you'll probably get innumerable examples.
                            Oh, their numerable, otherwise Google couldn't say "1-10 of 11234523" ;-)
                            As a programmer I just use one, two, three, allot so in my view it is 'innumerable' .
                            Dcitionary definition of innumerable
                            too many to be numbered.

                            So, if you have given them a number, they are no longer innumerable. ;-)

                            Edit: Especially when providing a total count.
                            • 11. Re: How to merge txt files
                              807603
                              I have written-edited the following code


                              import java.io.File;
                              import java.io.FileInputStream;
                              import java.io.FileOutputStream;
                              import java.io.FilenameFilter;
                              import java.io.IOException;
                              import java.io.InputStream;
                              import java.io.OutputStream;
                              
                              public class merging {
                              
                                   public static void main(String[] args) {
                                        File dir = new File(args[0]);                              
                                        File out = null;
                                        try {
                                             merge(dir,out);
                                        } catch (IOException e) {
                                             // TODO Auto-generated catch block
                                             e.printStackTrace();
                                        }
                                        
                                        
                                   }
                                   
                                   
                                   static void merge(File dir, File dst) throws IOException {
                                        String[] children = dir.list();
                                        FilenameFilter filter = new FilenameFilter() {
                                             public boolean accept(File dir, String name) {
                                                  return (name.endsWith(".txt") || name.endsWith(".TXT"));
                                             }
                                        };
                                       OutputStream out = new FileOutputStream(dst);   ///here i have the problem
                                       children = dir.list(filter);
                                       for (int i = 0; i < children.length; i++)          {   
                                       InputStream in = new FileInputStream(children); // Transfer bytes from in to out
                              byte[] buf = new byte[1024];
                              int len;
                              while ((len = in.read(buf)) > 0) {
                              out.write(buf, 0, len);
                              }
                              in.close();
                              }


                              out.close();
                              }
                              }
                              Any idea of the problem??
                              Thanx                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
                              • 12. Re: How to merge txt files
                                masijade
                                What problem? And what, exactly, does dst contain? Just a name, or a full path.
                                • 13. Re: How to merge txt files
                                  807603
                                  masijade. wrote:
                                  sabre150 wrote:
                                  As a programmer I just use one, two, three, allot so in my view it is 'innumerable' .
                                  Dcitionary definition of innumerable
                                  too many to be numbered.

                                  So, if you have given them a number, they are no longer innumerable. ;-)

                                  Edit: Especially when providing a total count.
                                  For many of us, counts become innumerable once we run out of fingers and toes.
                                  • 14. Re: How to merge txt files
                                    masijade
                                    petes1234 wrote:
                                    masijade. wrote:
                                    sabre150 wrote:
                                    As a programmer I just use one, two, three, allot so in my view it is 'innumerable' .
                                    Dcitionary definition of innumerable
                                    too many to be numbered.

                                    So, if you have given them a number, they are no longer innumerable. ;-)

                                    Edit: Especially when providing a total count.
                                    For many of us, counts become innumerable once we run out of fingers and toes.
                                    Well, I can count one further than that! 8-P
                                    1 2 Previous Next