Hello javaranch members.
I have a problem with java.io
import java.io.*;
class TestRandom
{
public static void main(
String[] args)
{
String s ="1234567";
String s1="HelloWeee";
String re=null;
try{
RandomAccessFile f = new RandomAccessFile("file.dat","rw");
System.out.println("File Pointer "+f.getFilePointer());
re=f.readLine();
while(re!=null)
{
System.out.println("File Pointer "+f.getFilePointer());
if(re.equals(s))
{
System.out.println("After read match lin file pointer" +f.getFilePointer());
f.seek((f.getFilePointer()-re.length()-1));
System.out.println("REpositioned file pointer" +f.getFilePointer());
b = s1.trim().getBytes();
f.write(b);
System.out.println("AFterwrite file pointer" +f.getFilePointer());
}
re=f.readLine();
}
}
catch(FileNotFoundException fnfe)
{
System.out.println("fnf");
}
catch(IOException i)
{
System.out.println("error");
}
}
}
My question is The input file is in this format
1000000
5555555
1234567
6666666
7777777
i am searching for a number 1234567. If i found i have to replace it with some other value HelloWeee.
when i ran my program
1000000
5555555
1HelloWeee666666
7777777
I couldnt understand why next line is getting deleted and coming to current line.
how to find and replace text in random file.
if source text is less than lenght of target text ...then will replacing causes the next coming lines to be overwritten.
I request you to reply as soon as possible
Thanks in advance