• Post Reply Bookmark Topic Watch Topic
  • New Topic

Please Please help. RandomAccess Files

 
Graham Porter
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i hope you can help me. I'm relatively new to Java but am learning. Anyway, i am trying to write some details into a file and then to retrieve these details and display them. I understand that the best way to do this is using RandomAccess as i need to access certain items of data stored. However i don't know how to do this, could anyone please help here, some code would be a great advantage. I know roughly how to create the file but i don't know how to write and retrieve data from it. IF you could help i would be very grateful.
Thanks in advance.
Graham
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best place to start is the tutorial from Sun. Explanations, example code, etc.:
http://java.sun.com/docs/books/tutorial/essential/io/index.html
 
Graham Porter
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the help, i appreciate it. I've gotten further, i can now write to a file using random access files. However i am having trouble reading from them, more specifically reading certain things. Any help on this matter would be great.
Thanks in advance.
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, Sun's tutorials are pretty good, for the most part. When you say you are having trouble reading from the file, could you be more specific? Code maybe?
 
Graham Porter
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, well to be more specific, i can read from the file but when i dispay the information on the console window there are spaces between each letter. Also how do i access a specific piece of information somewhere in the file, i know this can be done using Random Access but i can't do it. Also when writing to a file, how can i tell the program to write the data on the next line as opposed to somewhere on the same line. I've tried readLine() for getting on to the next line but it doesn't work, its not valid apparently.
Anyway thanks again in advance.
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know why your display if off, below code seems to work OK. If you still have a problem, post that code. You can use seek() to move to a particular position.
Wanting to move to the next line is a little weird. Usually you just move to a certain position, since you're not doing line input type stuff. I guess you could look at they bytes, determine when you've passed a CR/LF?

Hope this helps. Between the tutorial mentioned above, and the API for RandomAccessFile at http://java.sun.com/products/jdk/1.1/docs/api/java.io.RandomAccessFile.html , you should find all you need.
 
Graham Porter
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your help again but i have reached another problem. How do i locate a specific word in a file so that i can write it to the screen? I understand that you can find a byte and return data from that but how do i find out what byte position a word is at?
For example: If i had the following sentence:
"Java is complicated" in a file and say i wanted to return the word complicated, how would i do it without opening the file manually and counting the letters to find the position. Another thing that i mentioned in my other mail is that if i were to store that sentence, it would be stored like this: "j a v a i s c o m p l i c a t e d ." How do i get rid of the spaces?
The code i am using to write to the file is:
raf.writeChars(entity1 + ": ");
Any help you could give me would be greatly appreciated.
Graham.
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the API , the writeChars() method outputs a like the writeChar() method would. writeChar() says it writes a two-byte value, high byte first. Look at the writeBytes() method instead.
As far as searching a random access file for a string, I would imagine that's the way to do it. Don't think there's a shortcut.
 
Graham Porter
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much.
You were right, the writeBytes() got rid of the spaces in the strings.
However i am still unsure about locating a particular word within a file, you said that thats the way to do it but i don't know how. What did you mean?
Thanks again.
Graham.
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never tried to write one before, so don't think this is the preferred way to do it. However, it does work. I'm sure it could be improved.
 
Graham Porter
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi again, thank you so much for the help you gave there, its just what i was looking for, i only have one problem however.......i seem to be getting an error message saying that "sb.deleteCharAt(0);" is not a method in class String Buffer and i have had a look in the API and i have to agree with the message. You said that this works....is there something else i have to do to get this working?
Thanks again, i really appreciate this.
Graham.
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The api says that method has been around since 1.2. What version are you on? Maybe one could find a similar way to do it? I don't have the documentation for 1.1., but there's probably a way...
Unless you are forced to use an older Java version - upgrade - it's free
 
Graham Porter
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm using visual J++ 6.0 so i think its pretty new, erm....there doesn't seem to be any method in the String Buffer class to delets a character, i'm at a loss.
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Visual J++ ?? (Gasp!)
I was just looking at the api for 1.1 and that method is not there, it definately is in the modern version.
This is an ugly hack, but I think would work. Replace

with:

or maybe something similar. Like I said, I'm kinda winging this...
 
Graham Porter
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok...brilliant, that seems to work, however will this work for a large file or only for a limited number of chars???
Thanks
Graham.
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant? No. Quick little program that seems to work? Yes.
That's why I wanted to delete the character, what if you're searching through a giant file? If you are that toString() might get you in trouble also. I think one could think about this and make it better.
Your version of J++ looks like it is using 1.1 - that's what the cavemen used to program in That will solve that problem. To tell you the truth, I don't know. Maybe run it on some gigantic files and see when it breaks.
Also, you are probably not the first person to need something like this - maybe search around and try to find something small and fast.
Let me know. If this doesn't work and you can't find a good routine, I'll try and break and fix and break and fix until it ain't broke no more.
 
Graham Porter
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again, i've got a finished product at the moment, i will probably try and improve a few bits later but i thought i would send it to you if you were interested. As you can see you help was invaluable as a lot of the problems we spoke about are included. What it does is: asks the user for some details, it then reads these detalis into a file. (i haven't included a lot of that part.) the rest of it as you can see below asks for an OID and searches the file for this number, it then searches for the next occurance and returns everything between the two. It works for the moment but as i said i will probably tidy up some of it. Anyway thanks again.
try {
File f = new File("h:\\testout.txt");
RandomAccessFile raf = new
RandomAccessFile(f, "rw");
file://Read a character.
char ch = raf.readChar();
file://Seek to end of file.
raf.seek(f.length());
raf.writeByte(13);
file://Append to the end of file.
raf.writeBytes("OID=" + OID + " ");
raf.writeBytes("entity=" + entity1 + " ");
raf.writeBytes("Fname=" + Fname + " ");
raf.writeBytes("Lname=" + Lname + " ");
raf.writeBytes("DOB=" + DOB + " ");
raf.writeBytes("MatricCode=" + MatricCode + " ");
System.out.println("File now written.");
System.out.print("Please enter the OID of the details you want: ");
try {
lost = console.readLine();
}
catch (IOException e) { Lname = "<" <br /> + e + ">";}
raf.seek(0);
String searchString = (lost);
StringBuffer sb = new
StringBuffer();
boolean found = false;
while (!found && raf.getFilePointer() < raf.length())
{
sb.append((char)raf.read());
if (sb.toString().endsWith(searchString))
{
found = true;
}
}
if (found)
{
System.out.println("Found " + searchString + " ending at " + raf.getFilePointer());
}
else
{
System.out.println("Couldn't find " + searchString);
console.readLine();
}
String searchString2 = ("OID");
StringBuffer sb2 = new
StringBuffer();
boolean found2 = false;
long pos = raf.getFilePointer();
while (!found2 && raf.getFilePointer() < raf.length())
{
sb2.append((char)raf.read());
if (sb2.toString().endsWith(searchString2))
{
found2 = true;
}
}
long txtLength = (raf.getFilePointer()-4);
raf.seek(pos+1);
while (pos < txtLength)
{
System.out.print((char)raf.read());
pos++;
}
raf.close();
console.readLine();
}
catch (Exception e)
{
e.printStackTrace();
System.out.print("File not present!!!");
console.readLine();
}
}
 
Michael Hildner
Ranch Hand
Posts: 297
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you got something working. I'd be interested to see if the code breaks with a very large file. Best of luck.
 
YY MM
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good and tested function

public int findReplaceString(RandomAccessFile raf, String searchString, String replaceString)
{
int foundNum=0 ;
try
{
StringBuffer sb = new StringBuffer();
while (raf.getFilePointer() < raf.length())
{
sb.append((char)raf.read());

if (sb.length() > searchString.length())
{
sb.deleteCharAt(0);
}

if (sb.toString().equals(searchString))
{
foundNum++;
System.out.println("Found " + searchString + " ending at " + raf.getFilePointer());
raf.seek(raf.getFilePointer()-searchString.length());
raf.writeChars(replaceString);
int offsetLen=0;
if((2*replaceString.length())<searchString.length()){
offsetLen = searchString.length() - 2 * replaceString.length();
for(int i=0; i<offsetLen; i++)
raf.writeByte(0x20);
}

}
}//while loop

if(foundNum==0)
System.out.println("Couldn't find " + searchString);
else
System.out.println("find String times is "+foundNum);
}//end of try
catch (Exception e)
{
e.printStackTrace();
}
return foundNum;
} //end of findReplaceString
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!