Java Reference
In-Depth Information
Display 10.10
Redirecting Error Messages (part 2 of 2)
21 System.setErr(errStream);
22 System.err.println("Hello from System.err.");
23 System.out.println("Hello from System.out.");
24 System.err.println("Hello again from System.err.");
None of System.in , System.out , or
System.err needs to be closed, but the
streams you create should be explicitly
closed.
25 errStream.close();
26 }
27 }
F ILE errormessages.txt
Hello from System.err.
Hello again from System.err.
Screen Output
Hello from System.out.
10.3
The File Class
The scars of others should teach us caution.
SAINT JEROME
In this section, we describe the class File , which is not really an I/O stream class but
is often used in conjunction with file I/O. The class File is so important to file I/O
programming that it was even placed in the java.io package.
Programming with the File Class
The File class contains methods that allow you to check various properties of a file,
such as whether there is a file with a specified name, whether the file can be written to,
and so forth. Display 10.11 gives a sample program that uses the class File with text
files. (The class File works the same with binary files as it does with text files.)
Note that the File class constructor takes a name, known as the abstract name , as
an (string) argument. So the File class really checks properties of names. For example,
the method exists tests whether there is a file with the abstract name. Moreover, the
abstract name may be a potential directory (folder) name. For example, the method
isDirectory tests whether the abstract name is the name of a directory (folder). The
abstract name may be either a relative path name (which includes the case of a simple
file name) or a full path name.
Display 10.12 lists some of the methods in the class File .
abstract name
 
 
 
Search WWH ::




Custom Search