Merging/Appending two file in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • svpriyan
    New Member
    • Apr 2009
    • 18

    Merging/Appending two file in java

    Hai friends,
    I have two .ixf files
    My intention is to merge/ Append both to one.
    Could any one help me to find a way. i tried this forum though i coud not find anything yet.
    Thanks
    Priyan
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    First you need to know what an ixf file is. I think you can get the specification from the IBM DB2 sites.
    Then you can read it using the appropriate API.

    Comment

    • svpriyan
      New Member
      • Apr 2009
      • 18

      #3
      it does not matter, any two files. i wanted to append one to another.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        How you read/write to files in Java depends on the file formats of the files.

        Comment

        • svpriyan
          New Member
          • Apr 2009
          • 18

          #5
          Thanks for messages.
          i have 100s of files in Dir1 & Dir2. but they have same files, but contents are different. files have extension of *.ixf.

          I l explain here
          1- Read the contents of file1 which is in Dir1
          2- Read the contents of file2 which is in Dir2

          then add the contents of the file2 to the end of he file1 which is in Dir1.

          i tried some codes, which is not successful. if you can , guide with some codes, as i am new to Java, it will be a great hint for me to understand the problem.

          thank
          Priyan

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            What API are you using to read the files? Do you know the structure of the ixf files?

            Comment

            • svpriyan
              New Member
              • Apr 2009
              • 18

              #7
              NetBeans IDE 6.5

              the format looks like
              1., 3.
              4., 2.
              5., 4.
              9., 3.
              10., 6.
              12., 1.
              13., 1.
              14., 4.
              16., 10.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                NetBeans is just an IDE not an API.
                Are you sure that is the format? Verify the correct format by searching it from the IBM site.
                If the file is simple textual file like you are alluding to, then you can do this easily using FileReader and FileWriter API.

                Comment

                • svpriyan
                  New Member
                  • Apr 2009
                  • 18

                  #9
                  hai,
                  i able to come up to this part, and now i am worried where to give the file names.
                  if this part is ready, then we can move to the directory part.


                  Code:
                  
                  mport java.io.*;
                  import java.io.FileInputStream;
                   
                  public class CopyFile{
                  	private static void copyfile(String srFile, String dtFile){
                  		try{
                  			File f1 = new File(srFile);
                  			File f2 = new File(dtFile);
                  			InputStream in = new FileInputStream(f1);
                  			
                  			
                  			OutputStream out = new FileOutputStream(f2,true);
                   
                  			byte[] buf = new byte[8192];
                  			int len;
                  			while ((len = in.read(buf)) > 0){
                  				out.write(buf, 0, len);
                  			}
                  			in.close();
                  			out.close();
                  			System.out.println("File copied.");
                  		}
                  		catch(FileNotFoundException ex){
                  			System.out.println(ex.getMessage() + " in the specified directory.");
                  			System.exit(0);
                  		}
                  		catch(IOException e){
                  			System.out.println(e.getMessage());			
                  		}
                  	}
                  	public static void main(String[] args){
                  		switch(args.length){
                  			case 0: System.out.println("File has not mentioned.");
                  					System.exit(0);
                  			case 1: System.out.println("Destination file has not mentioned.");
                  					System.exit(0);
                  			case 2: copyfile(args[0],args[1]);
                  					System.exit(0);
                  			default : System.out.println("Multiple files are not allow.");
                  					  System.exit(0);
                  		}
                  	}
                  }

                  Comment

                  • svpriyan
                    New Member
                    • Apr 2009
                    • 18

                    #10
                    hai,

                    Actually its a query result which is stored in a file. query was executed with IBM DB2.

                    we don't need to worry about the content of the file that much. its just textual.
                    if you can give more suggestions with my code which i have given, might help me in get in further.
                    thnaks

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by svpriyan
                      hai,

                      Actually its a query result which is stored in a file. query was executed with IBM DB2.

                      we don't need to worry about the content of the file that much. its just textual.
                      if you can give more suggestions with my code which i have given, might help me in get in further.
                      thnaks
                      Appending a file to another one is easy: have a look at the constructors of the FileWriter class: two of them make the writer append to an already existing file.

                      kind regards,

                      Jos

                      Comment

                      Working...