• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

end of file and new IO

 
lydie prevost
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 476
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;

}
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11833
181
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Bourdeaux
Ranch Hand
Posts: 783
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andrew,

I wasn't aware of this. I think I will take a closer look at my instructions...
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic