problem reading&writing word documents

hi:

i wrote a program to look for a string and replace it with another. reading and wirting are done in the same file. it works fine as long as the file is a text document. if any one has any suggestion on how to make this program work for any file i would appreciate it. here is a portion of the codes that does the reading and wiriting. thanx.

f =new RandomAccessFile(aFile, mode);

int fLength = (int)f.length();

int bytesRead;

byte[] b;// = new byte[fLength];

String toWrite ="blood suckers";

String s;

try{

b =newbyte[fLength];

while (f.read() != -1)

f.read(b);

s =new String(b);

System.out.println(s);

long p =(long)s.indexOf(pattern);

String string1 = s.substring((int)p, (int)p+patternLength);

System.out.println(string1);

String string3 = s.substring((int)p+patternLength);

System.out.println(string3);

toWrite += string3;

byte[] aByte = toWrite.getBytes();

f.seek(p+1);

f.write(aByte);

[1678 byte] By [mushfaq] at [2007-9-26 7:29:20]
# 1

You can use StringBuffer instead of String because the String constructor use the locale translation and some bytes can be translated in the wrong way.

Or you can write a code which will wolk via the byte array and search for the desired pattern and place the part before the pattern into new byte array (which size is calculated and expanded dynamically), then the string for replacing and after that other part without the search pattern. Repeat it to the end of the original buffer.

The second way is more preferable :-))

Hope this can give you some ideas.

Regards,

Alex

akuznets at 2007-7-1 17:25:41 > top of Java-index,Archived Forums,Java Programming...