469,328 Members | 1,276 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

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
Apr 28 '09 #1
10 22075
r035198x
13,262 8TB
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.
Apr 28 '09 #2
it does not matter, any two files. i wanted to append one to another.
May 10 '09 #3
r035198x
13,262 8TB
How you read/write to files in Java depends on the file formats of the files.
May 11 '09 #4
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
May 11 '09 #5
r035198x
13,262 8TB
What API are you using to read the files? Do you know the structure of the ixf files?
May 11 '09 #6
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.
May 11 '09 #7
r035198x
13,262 8TB
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.
May 11 '09 #8
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.  
May 11 '09 #9
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
May 11 '09 #10
JosAH
11,448 Expert 8TB
@svpriyan
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
May 11 '09 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by William Ahern | last post: by
reply views Thread by Nathan Coast | last post: by
2 posts views Thread by Dave Taylor | last post: by
2 posts views Thread by Thanya Teutschbeim | last post: by
1 post views Thread by Emmanuel | last post: by
2 posts views Thread by Tom Costanza | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.