Connecting Tech Pros Worldwide Forums | Help | Site Map

Merging/Appending two file in java

Newbie
 
Join Date: Apr 2009
Posts: 18
#1: Apr 28 '09
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

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Apr 28 '09

re: Merging/Appending two file in java


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.
Newbie
 
Join Date: Apr 2009
Posts: 18
#3: May 10 '09

re: Merging/Appending two file in java


it does not matter, any two files. i wanted to append one to another.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: May 11 '09

re: Merging/Appending two file in java


How you read/write to files in Java depends on the file formats of the files.
Newbie
 
Join Date: Apr 2009
Posts: 18
#5: May 11 '09

re: Merging/Appending two file in java


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
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#6: May 11 '09

re: Merging/Appending two file in java


What API are you using to read the files? Do you know the structure of the ixf files?
Newbie
 
Join Date: Apr 2009
Posts: 18
#7: May 11 '09

re: Merging/Appending two file in java


NetBeans IDE 6.5

the format looks like
Quote:
1., 3.
4., 2.
5., 4.
9., 3.
10., 6.
12., 1.
13., 1.
14., 4.
16., 10.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#8: May 11 '09

re: Merging/Appending two file in java


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.
Newbie
 
Join Date: Apr 2009
Posts: 18
#9: May 11 '09

re: Merging/Appending two file in java


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.


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. mport java.io.*;
  4. import java.io.FileInputStream;
  5.  
  6. public class CopyFile{
  7.     private static void copyfile(String srFile, String dtFile){
  8.         try{
  9.             File f1 = new File(srFile);
  10.             File f2 = new File(dtFile);
  11.             InputStream in = new FileInputStream(f1);
  12.  
  13.  
  14.             OutputStream out = new FileOutputStream(f2,true);
  15.  
  16.             byte[] buf = new byte[8192];
  17.             int len;
  18.             while ((len = in.read(buf)) > 0){
  19.                 out.write(buf, 0, len);
  20.             }
  21.             in.close();
  22.             out.close();
  23.             System.out.println("File copied.");
  24.         }
  25.         catch(FileNotFoundException ex){
  26.             System.out.println(ex.getMessage() + " in the specified directory.");
  27.             System.exit(0);
  28.         }
  29.         catch(IOException e){
  30.             System.out.println(e.getMessage());            
  31.         }
  32.     }
  33.     public static void main(String[] args){
  34.         switch(args.length){
  35.             case 0: System.out.println("File has not mentioned.");
  36.                     System.exit(0);
  37.             case 1: System.out.println("Destination file has not mentioned.");
  38.                     System.exit(0);
  39.             case 2: copyfile(args[0],args[1]);
  40.                     System.exit(0);
  41.             default : System.out.println("Multiple files are not allow.");
  42.                       System.exit(0);
  43.         }
  44.     }
  45. }
  46.  
  47.  
Newbie
 
Join Date: Apr 2009
Posts: 18
#10: May 11 '09

re: Merging/Appending two file in java


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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#11: May 11 '09

re: Merging/Appending two file in java


Quote:

Originally Posted by svpriyan View Post

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
Reply

Tags
append, merge