This week's book giveaway is in the Python forum.
We're giving away four copies of Python Continuous Integration and Delivery and have Moritz Lenz on-line!
See this thread for details.
Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Help In RandomAccessFile  RSS feed

 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 9553
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the Java Documentation for java.io.RandomAccessFile:


A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. . . output operations write bytes starting at the file pointer and advance the file pointer past the bytes written.


A RandomAccessFile acts like an array of bytes in memory. If we had an array of bytes in memory:
byte arr[] = {1,2,3,4,5,6,7,8,9};
And we moved our pointer to arr[3] and write 0 to that position:
arr[3] = 0;
The array will look like this:
1,2,3,0,5,6,7,8,9
Not like this:
1,2,3,0,4,5,6,7,8,9
Which example above matches your assumptions and which matches what is really happening?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!