aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes end of file and new IO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "end of file and new IO" Watch "end of file and new IO" New topic
Author

end of file and new IO

lydie prevost
Ranch Hand

Joined: Mar 14, 2002
Posts: 32
Hello,
I am trying to find a way to detect the end of the file when going through the records in my find method.
I first tested on 1 ( I use raf ) but this way I cannot distinguish between a
deleted record and the end of the file. So I was thinking of using FileChannel to return the size of the file.
I have read somewhere ( I can't find it anymore) that SUN does not allow the use of NIO??? javascript: x()
I have nothing about this in my assignmemnt.
Though I have 3 questions?
1 ) Do you have a better way to get the number of records in the file?
( I don't think using EOFException is acceptable)
2) What is th edeal about NIO?
3) If NIO is OK can we use it to lock the record?

Thank you
- Lydie
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Originally posted by lydie prevost:
Hello,
I am trying to find a way to detect the end of the file when going through the records in my find method.
I first tested on 1 ( I use raf ) but this way I cannot distinguish between a
deleted record and the end of the file. So I was thinking of using FileChannel to return the size of the file.
I have read somewhere ( I can't find it anymore) that SUN does not allow the use of NIO??? javascript: x()
I have nothing about this in my assignmemnt.
Though I have 3 questions?
1 ) Do you have a better way to get the number of records in the file?
( I don't think using EOFException is acceptable)
2) What is th edeal about NIO?
3) If NIO is OK can we use it to lock the record?

Thank you
- Lydie


Calling this from a thread-safe context should do the trick:

private int getNumberRecords() throws IOException {
// stores value of schema length
final int headerLength = header.getHeaderLength();
// stores value for total record length
final int totalRecordLength = header.getTotalRecordLength();
// stores the value for length of all found records in data file
final long recordSectionLength = reader.length() - headerLength;
// checks for integrity of record data in data file
if ((recordSectionLength % totalRecordLength) != 0) {
throw new IOException("Corrupt data found in data file " +
header.getDataFile());
}

// stores number of records found in data file
final int numberRecords = (int) (recordSectionLength /
totalRecordLength);
return numberRecords;

}


Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
You cannot use NIO. The following is from Sun's Certification Website regarding the use of NIO in the SCJD :
The following APIs and facilities may not be used:

Enterprise JavaBeans
Servlets, JSP technology, or any other web-oriented APIs
NIO, the New IO facilities
Java DataBase Connectivity (JDBC) and SQL
Java IDL API and CORBA
Third party software libraries or tools (such as browsers)


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11484
    
  94

Hi Paul,

You cannot use NIO. The following is from Sun's Certification Website regarding the use of NIO in the SCJD : <cut>


We have had confirmation from Sun that this is on a per assignment basis. That is, if your instructions tell you that you must not use NIO, then you must not use NIO or you will fail. But if your instructions do not mention NIO at all, then you are free to use it.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Thanks Andrew,

I wasn't aware of this. I think I will take a closer look at my instructions...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: end of file and new IO