Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading two files and Writing into another single file

 
Praveen K Pippiri
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am reading two files,concatenating the complete data and inserting into new file.
I have written the following code.It is working for only Text format.Could you please tell me why this is not working for other formats like doc,rtf etc.Please advice me if you know a better way to join two files.
The code is as follows:
class FileTest
{
public static void main(String[] args)
{

try
{
String filename1=args[0];
String filename2=args[1];
String filename3=args[2];


File fileObj1 = new File(filename1);
File fileObj2 = new File(filename2);
File fileObj3 = new File(filename3);

FileInputStream fis1 = new FileInputStream(fileObj1);
BufferedInputStream bis1 = new BufferedInputStream(fis1);
int filelength1=(int)fileObj1.length();
byte data1[] = new byte[filelength1];
int noBytes=bis1.read(data1,0,filelength1);

fis1.close();
bis1.close();
fileObj1=null;
FileInputStream fis2 = new FileInputStream(fileObj2);
BufferedInputStream bis2 = new BufferedInputStream(fis2);
int filelength2=(int)fileObj2.length();
byte data2[] = new byte[filelength2];
noBytes=bis2.read(data2,0,filelength2);

fis2.close();
bis2.close();
fileObj2=null;
byte data3[] = new byte[filelength1+filelength2];

System.arraycopy(data1,0,data3,0,filelength1);
System.arraycopy(data2,0,data3,filelength1,filelength2);

FileOutputStream fos3 = new FileOutputStream(fileObj3,true);
fos3.write(data3);
fos3.close();
fileObj3=null;

}
catch(Exception e)
{
e.printStackTrace();
}
}
}
I tried the above by writing two times to the same file in the appended mode.Still it didn't work.Could you please suggest me where the problem lies in??
 
Praveen K Pippiri
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I figured out the problem.Basically it is retrieving the data+information associated to the data.When I am trying to merge the two files,MS word is not able to read the second file data.
May be I need to separate the data from the meta data..Can anybody give me suggestion how to separate the data from the meta data??
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are going to need some way to read and write the specific file types you are interested in. That is, you need to extract the information that is represented by that file type. Wnen you know what file type(s) you want to deal with, ask specifically (or try a google search).
Then, you have to figure out exactly what you really want to do. Text document formats such as rtf contain meta information, formatting, text, images, fonts, symbols, etc. You are going to have to decide what you mean when by "concatenating" two such files. Are you only going to keep the text or do you want to merge the formatting, font, etc information somehow? And once you know this you have to learn how to make that happen with the API you find to read/write these files.
Then, whatever you do will only work for that kind of file information. Try to use it to join two powerpoint or excel files and you have to just about start over.
 
Maybe he went home and went to bed. And took this tiny ad with him:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!