New to JavaProgramming Center
Java Platform Overview |
Getting Started |
Step-by-Step Programming
![]()
Contents Where should you write the code to open a file for reading?
The code to open a file should go in a For example:
try
{
// Code that opens a file for reading
}
catch (Exception e)
{
// Code to execute if an exception is thrown,
// such as if the file named does not exist.
}
Handling Exceptions
The classes that handle errors and exceptions are objects of the
The image below shows the hierarchy of the
How are you to know which classes to catch? You don't need to memorize these classes. There are a few ways to discover which classes you need to catch and possibly handle in your applications:
The last on the list is the easiest and most common way of determining which class or classes to catch. If you call methods from a class that throws an exception, the compiler displays a message letting you know. For instance, when writing code to read a file and close the file, you may get an error message like the following:
NorthPanel.java:58: unreported exception java.io.I/OException;
must be caught or declared to be thrown
br.close();
^
1 error
In addition to telling you what kind of exception you need to catch, the compiler lists which method is
causing the problem. In this case, Throws: I/OException - If an I/O error occurs Read what the other methods of this class throw. After working with Java classes for a while, you'll become familiar with which classes need to be caught and handled, and which you can declare thrown, which you'll learn about later. Now that you know what kind of error or condition object you need to catch:
In other words, place the code that can throw an exception
in the For instance, here is a defined method that contains code that may cause some condition to occur. Suppose you want to read a file. The pseudo code demonstrates:
public void doReadMethod() {
try
{ // Open try clause
Code that attempts to read a particular file
} // Close try clause
// If there is a problem and an exception is thrown:
catch (IOException ioe)
{ //Open catch
System.out.println("An exception was thrown: " + ioe);
} // Close catch
}
The There is more to exception handling, but for this class and for basic reading from and writing to files, these fundamentals are enough for now. Java Input and OutputNow that you understand the basics of how error and exception objects are created, thrown, and handled, you can move onto reading from and writing to files.
The So far you instantiated or created objects for everything in this application from the buttons, to the text fields, to actions and events. Reading from and writing to files also involves objects, specifically objects called streams. Think of streams like the bridges. At one end of the bridge is the starting point object, and at the other a destination object. The bridge is the object a vehicle uses to cross from one side of a body of water to the other. Streams serve a similar function. The Java platform includes two hierarchies of classes that support streams:
In the use of stream objects, characters or bytes are read or written in sequential order, character by
character, or byte by byte. Because the Character streams can work on input and output to convert each character from the encoding scheme of a native operating system to the Unicode character set used by the Java platform and full support internationalization.
Reading from a file requires three basic steps:
What you do with the characters is up to you. You can send them to the console, display them in a GUI, or write them to a file or database. The steps above involve classes that require exception handling as well.
Like creating other objects, you create a stream object by instantiating one of the many stream classes. First, though, you may need information about the file you want to read, such as if the file
exists or when it was last modified. To perform checks on a file or directory, associate the file with an
instance of the
File f = new File("myfile.txt");
A
The documentation contains more methods that are worthy of noting.
| |||||||||||||||||
|
| ||||||||||||