13.07.2015 Views

I/O Fundamentals

I/O Fundamentals

I/O Fundamentals

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Java Input and Outputonly by your application, you have no concerns.• Write native methods to perform all input and output for the files in question.Because the native programs "do the right thing on the current platform", youagain have no worry. However, it is important to note that you will need todeliver a different executable for each target platform. Also, this solution willnot work for unsigned applets.• If you know the Endianness of the platform or the file being read, you couldwrite a translation filter for input or output.• Use combinations of the above approaches.For further information and some utilities to assist, look up "Endian" in RoedyGreen's Java Glossary (http://mindprod.com/gloss.htmlInput/Output ExceptionsUntil this point, we have avoided the topic of exception handling in I/Ooperations. So far, all of our example code has been of the form:try {// do some I/O operation(s)}catch(Exception e) {e.printStackTrace();}While this simplifies the examples significantly, it is hardly appropriate for realprogramming.The Java core libraries define several I/O exceptions, all of which extend a classnamed IOException. IOException directly extends Exception; that means thatit is not a "runtime exception". As a result, you must always either provide trycatchblocks around any I/O code, or add a throws clause to the method thatperforms the I/O. Our example above shows a simple try-catch situation, whilethe following code demonstrates a throws clause:public void doSomeIO() throws IOException {// do some I/O operation(s)// note that we're not in a try-catch block!}Of course we're now requiring any code that calls doSomeIO() to either surroundit with a try-catch block or add a throws clause to that method.Java Input and Output -56© 1996-2003 jGuru.com. All Rights Reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!