• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Help In RandomAccessFile

 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic