• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Help on file pointer after seek()  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is taken from JavaCaps:
15. If raf is a RandomAccessFile, what is the result of compiling and executing the following code? raf.seek( raf.length() );
[a] The code will not compile.
[b] An IOException will be thrown.
[c] The file pointer will be positioned immediately before the last character of the file.
[d] The file pointer will be positioned immediately after the last character of the file.
[e] None of the above
The answer is c. But I did a small program to test it.
import java.io.*;
public class TestApp {
public static void main(String args[])
throws IOException {
RandomAccessFile file = new RandomAccessFile("test_raf.txt", "rw");
file.writeByte(1);
file.writeByte(2);
file.writeByte(3);
file.writeByte(4);
System.out.println("length = " + file.length());
file.seek(0);
System.out.println(file.readByte());
file.seek(1);
System.out.println(file.readByte());
file.seek(2);
System.out.println(file.readByte());
file.seek(3);
System.out.println(file.readByte());
//file.seek(4);
//System.out.println(file.readByte());
file.close();
}
}
The output is:
length = 4
1
2
3
4
Runtime exception will occur if the last two lines are uncommented.
Therefore, I think that the answer be d instead of c. Am I right? Thanks!
Summer
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are right, I have created a small test program to test the seek function.
after executing the line file.seek(file.length()) then trying to read the next byte, it gives me an EOF Exception. which means that the seek function position the file pointer just after the last character in the file not before.
this is the main method
 
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SummerPing and Maitham,
Welcome to Javaranch
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.
 
Summer Ping
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maitham,
Thank you for your help.
Summer
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the answer should be b and d, because an IOException is thrown when you try to read the last time.How many options did you have to choose?
Francisco
[ July 16, 2002: Message edited by: Francisco A Guimaraes ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!