This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 9521
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!