File IO - Character streams & Line oriented IO

Published: 22 April 2020
on channel: Soumyashree Sahoo
133
0

Character Streams:
Used to read /write character data.

All character stream classes are descended from Reader and Writer. As with byte streams, there are character stream classes that specialize in file I/O: FileReader and FileWriter.

Class Reader: class used for reading character streams.
Class Writer: class used for writing to character streams

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.

We have to use two classes we haven't seen before, BufferedReader and PrintWriter.

Class BufferedReader:


Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.

In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,

BufferedReader in = new BufferedReader(new FileReader("foo.in"));



will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient.


Class PrintWriter:
public class PrintWriter extends Writer

Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream.


Watch video File IO - Character streams & Line oriented IO online without registration, duration hours minute second in high quality. This video was added by user Soumyashree Sahoo 22 April 2020, don't forget to share it with your friends and acquaintances, it has been viewed on our site 133 once and liked it 0 people.