• Post Reply Bookmark Topic Watch Topic
  • New Topic

Seek()  RSS feed

 
Midhat Mukadam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What role does seek plays in this...... i cudnt get wt seek actually is....... Thank u..
 
Henry Wong
author
Sheriff
Posts: 23282
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Midhat Mukadam wrote:import java.io.*;
class testRandomFile
{
public static void main(String args[])
{
RandomAccessFile file=null;
try
{
file=new RandomAccessFile("work.txt","rw");
file.writeInt(85);
file.writeChar('V');
file.seek(0);
System.out.println(file.readInt());
System.out.println(file.readChar());
file.seek(file.length());
file.writeBoolean(true);
file.seek(3);
System.out.print(file.readBoolean());
file.close();
}
catch(IOException e)
{
System.out.println("Exception " +e);





}

}
}
What role does seek plays in this...... i cudnt get wt seek actually is....... Thank u..


First, this topic is not related to threads -- let me move it to the I/O forum for you.

Second, have you read the javadoc for RandomAccessFile? What is it that you don't understand?

If you tell us what you don't understand, perhaps we can elaborate.

Henry
 
Midhat Mukadam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i know...it is not the part of threads...it is of the io streams...... i dont knw...but even after reading about seek method...i coudnt get what it is actually....... can you please explain me with a certain example or something
 
Tony Docherty
Bartender
Posts: 3264
81
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I can explain it any better than the API docs but here goes;

A RandomAccessFile treats a file on disk like a large byte array. You can access any byte in the file by specifying an offset from the beginning of the file in the same way you can access any element in an array by specifying a 0 based index. In a RandomAccessFile, the method you use to specify this offset is seek(). When you call seek() with a value it positions the file pointer to that location and subsequent reads and/or writes happen relative to the current file pointer location. Note, reads and writes will advance the file pointer to the next location after the bytes read/written. So if you want to read the 10th byte in the file call seek(10) and then call read().
 
Henry Wong
author
Sheriff
Posts: 23282
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Midhat Mukadam wrote:yes i know...it is not the part of threads...it is of the io streams...... i dont knw...but even after reading about seek method...i coudnt get what it is actually....... can you please explain me with a certain example or something


No need for an example, the concept is pretty easy. Basically, the way to locate data on a file is via an index. The beginning of the file is location zero. Ten bytes in is location ten. Two kilobytes from the beginning is 2 times 1K, or 2 times 1024 bytes, or location 2048. Etc. Etc. Etc.

When you first open a file, it is at location zero, and as you read the file, the location index increments. However, what if you don't want to read from the beginning? What if you want to move around the file? To do that you seek to the particular location via the seek() method.


And next time, please be more careful. If you know that it is not threads related, please don't put it in the threads forum -- please put it in the correct forum.

Henry
 
Midhat Mukadam
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya ya fine fine..i will be careful.... thank you so much for explaining me so well......!.....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!