File IO - Character streams & Line oriented IO

Опубликовано: 22 Апрель 2020
на канале: 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.


Смотрите видео File IO - Character streams & Line oriented IO онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Soumyashree Sahoo 22 Апрель 2020, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 133 раз и оно понравилось 0 людям.