How to append text in a text file at a specific position

I have a html file in which i want to append a particular line of code after the first statement that contains a <script> tag.

How do i achieve this?

I am able to append at the end of the file but could not find a way to append at a particular position in the html file.

Can anyone help me with this?

[331 byte] By [Neha@81a] at [2007-10-3 6:41:53]
# 1

> I have a html file in which i want to append a

> particular line of code after the first statement

> that contains a <script> tag.

> How do i achieve this?

> I am able to append at the end of the file but could

> not find a way to append at a particular position in

> the html file.

> Can anyone help me with this?

You can't insert data at a specific location. You need to read in the data after the location you want to write to, write your data, and then append the data that you have read in.

Kaj

kajbja at 2007-7-15 1:31:03 > top of Java-index,Java Essentials,Java Programming...
# 2
Hi Kaj,Can you provide me with the code for this?Do you mean that i first i have to find out the loaction where i have to append and then read the data and then append it to the file ?
Neha@81a at 2007-7-15 1:31:03 > top of Java-index,Java Essentials,Java Programming...
# 3

> Hi Kaj,

> Can you provide me with the code for this?

> Do you mean that i first i have to find out the

> loaction where i have to append and then read the

> data and then append it to the file ?

Sorry, but I will not post code for that.

This is what you need to do.

1) Find the location where you want to write

2) Read the data after that location.

3) Write your new data to the location.

4) Write down the old data to the location just after where you wrote the new data

Kaj

kajbja at 2007-7-15 1:31:03 > top of Java-index,Java Essentials,Java Programming...
# 4
maybe,java.io.RandomAccessFile
mchan0a at 2007-7-15 1:31:03 > top of Java-index,Java Essentials,Java Programming...
# 5
It's almost always easier to create a new version of the file. Copy up to the insertion point, add the inserted text, then copy the rest of the original file. Then, if necessary, rename your input and output files so the new takes the place of the old.
malcolmmca at 2007-7-15 1:31:03 > top of Java-index,Java Essentials,Java Programming...
# 6
and no java even, egfind -type f -name '*' -print | while read i doecho $icat $i | sed 's/foo/bar/g' > $i.outmv $i.out $i done
mchan0a at 2007-7-15 1:31:03 > top of Java-index,Java Essentials,Java Programming...
# 7

Open a Randomaccessfile and keep track of the filepointer. You can then seek to the location you want and write there.

The following code creates a RandomAccessFile to read the file named farrago.txt:

new RandomAccessFile("xanadu.txt", "r");

And this one opens the same file for both reading and writing:

new RandomAccessFile("xanadu.txt", "rw");

After the file has been opened, you can use the common read or write methods defined in the DataInput and DataOutput interfaces to perform I/O on the file.

RandomAccessFile supports the notion of a file pointer. The file pointer indicates the current location in the file. When the file is first created, the file pointer is set to 0, indicating the beginning of the file. Calls to the read and write methods adjust the file pointer by the number of bytes read or written.

In addition to the normal file I/O methods that implicitly move the file pointer when the operation occurs, RandomAccessFile contains three methods for explicitly manipulating the file pointer.

int skipBytes(int) ?Moves the file pointer forward the specified number of bytes

void seek(long) ?Positions the file pointer just before the specified byte

long getFilePointer() ?Returns the current byte location of the file pointer

hifi_kodera at 2007-7-15 1:31:03 > top of Java-index,Java Essentials,Java Programming...