Author |
Message
|
bhavyabhandari |
Posted: Tue May 22, 2007 9:26 am Post subject: How to append a text file in between an existing file |
|
|
Apprentice
Joined: 09 Nov 2006 Posts: 33
|
I have a query ..
I have a text file say a.txt which contains a header,data and a trailer.
I have another text file say b.txt which has some data,
Now i have to append the content of b.txt below the data part of a.txt
using Java compute node in Messg Broker..
How should i go about to append the file b.txt in between the file a.txt.. |
|
Back to top |
|
 |
jbanoop |
Posted: Tue May 22, 2007 9:29 am Post subject: |
|
|
Chevalier
Joined: 17 Sep 2005 Posts: 401 Location: SC
|
Use the file handling API of Java. Open the target file in read-write mode, move to the location after which you need to do the inserts .. write the contents you want to the file (by reading from the other file and writing to the target).
Should be possible.
Anoop |
|
Back to top |
|
 |
bhavyabhandari |
Posted: Tue May 22, 2007 9:33 am Post subject: The code.. |
|
|
Apprentice
Joined: 09 Nov 2006 Posts: 33
|
The code I have used ::
int count = 0;
FileReader fileReader = new FileReader(record);
BufferedReader br = new BufferedReader(fileReader);
while ((record = br.readLine()) != null)
{
count++;
}
System.out.println(count); // gives the total no. of,lines.
count = count-2; // the line where i have to append the text file b.txt
How should i append it here?? |
|
Back to top |
|
 |
jbanoop |
Posted: Tue May 22, 2007 9:41 am Post subject: |
|
|
Chevalier
Joined: 17 Sep 2005 Posts: 401 Location: SC
|
|
Back to top |
|
 |
bhavyabhandari |
Posted: Tue May 22, 2007 9:51 am Post subject: a little more help |
|
|
Apprentice
Joined: 09 Nov 2006 Posts: 33
|
Could you help me a bit to explain it through a coded perspective if possible.. |
|
Back to top |
|
 |
jbanoop |
Posted: Tue May 22, 2007 10:18 am Post subject: |
|
|
Chevalier
Joined: 17 Sep 2005 Posts: 401 Location: SC
|
* read contents of b.txt into a byte[] using regular file read operations.
* create new RandomAccessFile for a.txt opened in "rw" mode
* move file pointer (seek(int numBytes) ) to the location you want to insert the new content
* insert content using write(byte[]) method.
Anoop |
|
Back to top |
|
 |
|