The Java platform stores character values using Unicode conventions Character stream I/O automatically translates this internal format to and from the local character set. In Western locales, the local character set is usually an 8-bit superset of ASCII.For most applications, I/O with character streams is no more complicated than I/O with byte streams. Input and output done with stream classes automatically translates to and from the local character set. A program that uses character streams in place of byte streams automatically adapts to the local character set and is ready for internationalization — all without extra effort by the programmer.
If internationalization isn't a priority, you can simply use the character stream classes without paying much attention to character set issues. Later, if internationalization becomes a priority, your program can be adapted without extensive recoding. See the Internationalization trail for more information.
Using Character Streams
All character stream classes are descended fromReaderandWriter. As with byte streams, there are character stream classes that specialize in file I/O:FileReaderandFileWriter. TheCopyCharactersexample illustrates these classes.import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class CopyCharacters { public static void main(String[] args) throws IOException { FileReader inputStream = null; FileWriter outputStream = null; try { inputStream = new FileReader("xanadu.txt"); outputStream = new FileWriter("characteroutput.txt"); int c; while ((c = inputStream.read()) != -1) { outputStream.write(c); } } finally { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } } } }CopyCharactersis very similar toCopyBytes. The most important difference is thatCopyCharactersusesFileReaderandFileWriterfor input and output in place ofFileInputStreamandFileOutputStream. Notice that bothCopyBytesandCopyCharactersuse anintvariable to read to and write from. However, inCopyCharacters, theintvariable holds a character value in its last 16 bits; inCopyBytes, theintvariable holds abytevalue in its last 8 bits.Character Streams that Use Byte Streams
Character streams are often "wrappers" for byte streams. The character stream uses the byte stream to perform the physical I/O, while the character stream handles translation between characters and bytes.FileReader, for example, usesFileInputStream, whileFileWriterusesFileOutputStream.There are two general-purpose byte-to-character "bridge" streams:
InputStreamReaderandOutputStreamWriter. Use them to create character streams when there are no prepackaged character stream classes that meet your needs. The sockets lesson in the networking trail shows how to create character streams from the byte streams provided by socket classes.Line-Oriented I/O
Character I/O usually occurs in bigger units than single characters. One common unit is the line: a string of characters with a line terminator at the end. A line terminator can be a carriage-return/line-feed sequence ("\r\n"), a single carriage-return ("\r"), or a single line-feed ("\n"). Supporting all possible line terminators allows programs to read text files created on any of the widely used operating systems.Let's modify the
CopyCharactersexample to use line-oriented I/O. To do this, we have to use two classes we haven't seen before,BufferedReaderandPrintWriter. We'll explore these classes in greater depth in Buffered I/O and Formatting. Right now, we're just interested in their support for line-oriented I/O.The
CopyLinesexample invokesBufferedReader.readLineandPrintWriter.printlnto do input and output one line at a time.Invokingimport java.io.FileReader; import java.io.FileWriter; import java.io.BufferedReader; import java.io.PrintWriter; import java.io.IOException; public class CopyLines { public static void main(String[] args) throws IOException { BufferedReader inputStream = null; PrintWriter outputStream = null; try { inputStream = new BufferedReader(new FileReader("xanadu.txt")); outputStream = new PrintWriter(new FileWriter("characteroutput.txt")); String l; while ((l = inputStream.readLine()) != null) { outputStream.println(l); } } finally { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } } } }readLinereturns a line of text with the lineCopyLinesoutputs each line usingprintln, which appends the line terminator for the current operating system. This might not be the same line terminator that was used in the input file.There are many ways to structure text input and output beyond characters and lines. For more information, see Scanning and Formatting.