| 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: 473
|
|
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<br /><i>anton.golovin@gmail.com</i><br />SCJP, SCJD, SCBCD, SCWCD
|
 |
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: 10892
|
|
Hi Paul,
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...
|
 |
 |
|
|
subject: end of file and new IO
|
|
|