• Post Reply Bookmark Topic Watch Topic
  • New Topic

Seek Method for files(Urgent)  RSS feed

 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have huge file on serverside. Depends on client request, server will get number of bytes(location of the file where server needs to start).
(1) How to seek location of the file(by number of bytes) and start reading from that location?
(2) How to seek location of file(by number of byte) and start reading previous records of location of file?
Is Random File Access class will be helpful???
Thanks,
Angela
 
Ian Montgomery
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're on the right track. Check out the seek() function of RandomAccessFile directly from Sun.
http://java.sun.com/j2se/1.3/docs/api/java/io/RandomAccessFile.html#seek
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela,
U can do that using RandomAccessFile.For that, u can make use of the methods like seek(long l), getFilePointer() etc. U can set the file offset using the seek(long l) method. To find out the offset, u can use the mathod getFilePointer(). That way, u can read a file for a specified number of bytes or from a specific file pointer to another.
Hope this will help u.
---------
Nayan
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all. But now the need is to seek for specific words instead seek for byte location. Is still RandomAccessFile will be helpful??? If yes, how?
Thanks,
Angela
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela,
I think, u can use String class methods to seek for specific words location. RandomAccessFile won't be helpful for that matter.
--------------
Nayan
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nayanjyoti Talukdar:
Hi Angela,
I think, u can use String class methods to seek for specific words location. RandomAccessFile won't be helpful for that matter.
--------------
Nayan

Do you have example of above description?
Also, again I am repeating program needs:
(1) Add new data at the end of the file?
(2) Search for specific word from file?Also,get it's byte location.
(3) From 0 byte location and byte location of specific word, get the data and copy that data to other file.
What is best solution of this senario?
Thanks,
Angela
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean, put the whole file in a String object. Now, u can search for the keyword using the indexOf() method of String class. That will give u the position of the keyword(u are looking for). Now, get the data whole data using the location and append that in u'r file. For that, u can use of substring(int,int) method.
Does this help u?
-----------
Nayan
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
Does RandomAccessFile takes more memory when we use? Also is it fast to seek?
Thanks again,
Angela
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure how much memory RandomAccessFile uses, but it's pretty slow compared to using streams. At least, it was for JDK 1.2 - it may have imporved since then. But if speed is an issue (which it may be, if the file is "huge") then you'll probably get better results with streams. See my previous posts here and here for more info.
Nowadays it's probably better to learn to use the various methods of java.nio and its subpackages. From a FileInputStream or RandomAccessFile you can get a FileChannel, which has a position(long) method you can use to move backwards or forwards in the file. Various other classes and methods can be used to read bytes as necessary.
Be aware that whatever you do here, you will probably also need to know something about the character encoding used in the file. In particular you will need to know if it's in something like UTF-8, where each char is 1-3 bytes and when you set the position() you don't know in advance if you've arrived in the middle of a multi-byte sequence. This required extra attention if you're using UTF-8. For other encodings which use a constant number of bytes per char, it's simpler to jump around without having to worry about accidentally landing midway through a character.
The hardest part of what you describe may be searching backwards for previous records. How are records separated in your file? Do they have constant length? Is each record on a separate line? Are there even distinct "records", or is the whole file just a bunch of text? The answers to these questions will help determine how you can best search for things in the file.
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:

The hardest part of what you describe may be searching backwards for previous records. How are records separated in your file? Do they have constant length? Is each record on a separate line? Are there even distinct "records", or is the whole file just a bunch of text? The answers to these questions will help determine how you can best search for things in the file.

Each record contains LastName, FirstName, EmailAddress, CompanyName. I store this values as Hash key and value pair e.g LASTNAME=D'souza
and then I add Hashtable(which consist of lastname, firstname, email, companyname) in file called mgmt.txt. This file will not fixed size.So file will looks like following:
ISGROUP=3000
LASTNAME=5300680061006800
FIRSTNAME=41006e0075006a006100
EMAILADDRESS=61006e0075006a0040006b0063002e006f006d00
INGROUP=3000
COMPANYNAME=7400650073007400
DONE=DONE
ISGROUP=3000
LASTNAME=5300680061006800
FIRSTNAME=41006e0075006a006100
EMAILADDRESS=61006e0075006a0040006b0063002e006f006d00
INGROUP=3000
COMPANYNAME=7400650073007400
DONE=DONE
--DONE is delimeter to separate each record
Also I create another file called index.txt which is fixed size, which will contain record number & it's location in mgmt.txt file. So when I do search as it is fixed size file(index.txt) I can directly jump to that record and get byte location of that record in mgmt.txt. Then I can go in mgmt.txt file on that record location and get the record values.
Is this good idea?
Also, sometime I will need to copy or delete specific records?
Thanks,
Angela
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The index file idea sounds reasonable. I'm suspicious about the mgmt.txt file format you've shown. Why are all the values shown as numbers? Wouldn't it make more sense to have something like
LASTNAME=Yingst
FIRSTNAME=Jim
rather than
LASTNAME=5300680061006800
FIRSTNAME=41006e0075006a006100
? Did you generate this file yourself, or did it come from somewhere else? If you generated it yourself, I think you might need to work on how to print characters in a file.
Also, you talk about a Hashtable. You're not referring to an actual java.util.Hashtable, are you? We're talking about a file format, right? Or is there an actual java.util.Hashtable object somewhere that you're putting this data in?
 
Angela D'souza
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Why are all the values shown as numbers? Wouldn't it make more sense to have something like
LASTNAME=Yingst
FIRSTNAME=Jim
rather than
LASTNAME=5300680061006800
FIRSTNAME=41006e0075006a006100
? Did you generate this file yourself, or did it come from somewhere else? If you generated it yourself, I think you might need to work on how to print characters in a file.
Also, you talk about a Hashtable. You're not referring to an actual java.util.Hashtable, are you? We're talking about a file format, right? Or is there an actual java.util.Hashtable object somewhere that you're putting this data in?

The reason I have numbers(encoded) is for security purpose.
Yes, first I am putting data like firstname, lastname, email, companyname in java.util.Hashtable and then I write Hashtable
into file. I have function to write Hashtable in to file.
Plz give me good suggestions here.
Thanks,
Angela
P.S: I have to have number system for security purpose in file.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!