Java Reference
In-Depth Information
Thefollowingexampleusesthesecondconstructortocreateabridgetoanunderlying
file input stream so that Polish text can be read from an ISO/IEC 8859-2-encoded file.
FileInputStream fis = new FileInputStream("polish.txt");
InputStreamReader
isr
=
new
InputStreamReader(fis,
"8859_2");
char ch = isr.read(ch);
Note
OutputStreamWriter
and
InputStreamReader
declare a
String
getEncoding()
method that returns the name of the character encoding in use.
Whentheencodinghasahistoricalname,thatnameisreturned;otherwise,theencod-
ing's canonical name is returned.
FileWriter and FileReader
FileWriter
is a convenience class for writing characters to files. It subclasses
OutputStreamWriter
, and its constructors call
Out-
putStreamWriter(OutputStream)
.Aninstanceofthisclassisequivalenttothe
following code fragment:
FileOutputStream fos = new FileOutputStream(pathname);
OutputStreamWriter osw;
osw
=
new
OutputStreamWriter(fos,
Sys-
tem.getProperty("file.encoding"));
incorporate file-writing code.
Listing 8-19
addresses this situation by presenting a re-
vised
File
class that uses
FileWriter
to log messages to a file.
Listing 8-19.
Logging messages to an actual file
package logging;
import java.io.FileWriter;
import java.io.IOException;
class File implements Logger
{
private final static String LINE_SEPARATOR =
System.getProperty("line.separator");
private String dstName;