Java readers and streams

Java IO: Readers and Writers

The Java Reader ( java.io.Reader ) and Java Writer class ( java.io.Writer ) in Java IO work much like the InputStream and OutputStream with the exception that Reader and Writer are character based. They are intended for reading and writing text. The InputStream and OutputStream are byte based, remember?

Reader

The Java Reader is the base class of all Reader ‘s in the Java IO API. Subclasses include a BufferedReader , PushbackReader , InputStreamReader , StringReader and several others.

Here is a simple Java IO Reader example:

Reader reader = new FileReader(«c:\\data\\myfile.txt»); int data = reader.read(); while(data != -1)

Notice, that while an InputStream returns one byte at a time, meaning a value between 0 and 255 (or -1 if the stream has no more data), the Reader returns a char at a time, meaning a value between 0 and 65535 (or -1 if the stream has no more data). This does not necessarily mean that the Reader reads two bytes at a time from the source it is connected to. It may read one or more bytes at a time, depending on the encoding of the text being read.

Combining Readers With InputStreams

A Java Reader can be combined with an InputStream . If you have an InputStream and want to read characters from it, you can wrap it in an InputStreamReader . Pass the InputStream to the constructor of the InputStreamReader like this:

Reader reader = new InputStreamReader(inputStream);

In the constructor you can also specify what character set to use to decode the text etc. More on that in the text on InputStreamReader .

Writer

The Java Writer class is the base class of all Writer s in the Java IO API. Subclasses include BufferedWriter and PrintWriter among others.

Читайте также:  Resolve dependencies in java

Here is a simple Java IO Writer example:

Writer writer = new FileWriter("c:\\data\\file-output.txt"); writer.write("Hello World Writer"); writer.close();

Combining Writers With OutputStreams

A Java Writer can be combined with an OutputStream just like Readers and InputStream ‘s. Wrap the OutputStream in an OutputStreamWriter and all characters written to the Writer are passed on to the OutputStream . Here is an OutputStreamWriter example:

Writer writer = new OutputStreamWriter(outputStream);

Combining Readers and Writers

Just like with streams, Reader ‘s and Writer ‘s can be combined into chains to achieve more interesting IO. It works just like combining the Reader with InputStream ‘s or the Writer with OutputStream ‘s. For instance, you can achieve buffering by wrapping a Reader in a BufferedReader , or a Writer in a BufferedWriter . Here are two such examples:

Reader reader = new BufferedReader(new FileReader(. )); Writer writer = new BufferedWriter(new FileWriter(. ));

Источник

Java IO Overview

In this text I will try to give you an overview of the classes in the Java IO ( java.io ) package. More specifically, I will try to group the classes after their purpose. This grouping should make it easier for you in the future, to determine the purpose of a class, or find the class you need for a specific purpose.

Input and Output — Source and Destination

The terms «input» and «output» can sometimes be a bit confusing. The input of one part of an application is often the output of another. Is an OutputStream a stream where output is written to, or output comes out from (for you to read)? After all, an InputStream outputs its data to the reading program, doesn’t it? Personally, I found this a bit confusing back in the day when I first started out learning about Java IO.

In an attempt to clear out this possible confusion, I have tried to put some different names on input and output to try to link them conceptually to where the input comes from, and where the output goes.

Читайте также:  Оформление блоков при помощи css

Java’s IO package mostly concerns itself with the reading of raw data from a source and writing of raw data to a destination. The most typical sources and destinations of data are these:

  • Files
  • Pipes
  • Network Connections
  • In-memory Buffers (e.g. arrays)
  • System.in, System.out, System.error

The diagram below illustrates the principle of a program reading data from a source and writing it to some destination:

Streams

IO Streams are a core concept in Java IO. A stream is a conceptually endless flow of data. You can either read from a stream or write to a stream. A stream is connected to a data source or a data destination. Streams in Java IO can be either byte based (reading and writing bytes) or character based (reading and writing characters).

The InputStream, OutputStream, Reader and Writer

A program that needs to read data from some source needs an InputStream or a Reader. A program that needs to write data to some destination needs an OutputStream or a Writer . This is also illustrated in the diagram below:

An InputStream or Reader is linked to a source of data. An OutputStream or Writer is linked to a destination of data.

Java IO Purposes and Features

Java IO contains many subclasses of the InputStream , OutputStream , Reader and Writer classes. The reason is, that all of these subclasses are addressing various different purposes. That is why there are so many different classes. The purposes addressed are summarized below:

  • File Access
  • Network Access
  • Internal Memory Buffer Access
  • Inter-Thread Communication (Pipes)
  • Buffering
  • Filtering
  • Parsing
  • Reading and Writing Text (Readers / Writers)
  • Reading and Writing Primitive Data (long, int etc.)
  • Reading and Writing Objects

These purposes are nice to know about when reading through the Java IO classes. They make it somewhat easier to understand what the classes are targeting.

Java IO Class Overview Table

Having discussed sources, destinations, input, output and the various IO purposes targeted by the Java IO classes, here is a table listing most (if not all) Java IO classes divided by input, output, being byte based or character based, and any more specific purpose they may be addressing, like buffering, parsing etc.

Читайте также:  Java generics return this
Byte Based Character Based
Input Output Input Output
Basic InputStream OutputStream Reader
InputStreamReader
Writer
OutputStreamWriter
Arrays ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter
Files FileInputStream
RandomAccessFile
FileOutputStream
RandomAccessFile
FileReader FileWriter
Pipes PipedInputStream PipedOutputStream PipedReader PipedWriter
Buffering BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
Filtering FilterInputStream FilterOutputStream FilterReader FilterWriter
Parsing PushbackInputStream
StreamTokenizer
PushbackReader
LineNumberReader
Strings StringReader StringWriter
Data DataInputStream DataOutputStream
Data — Formatted PrintStream PrintWriter
Objects ObjectInputStream ObjectOutputStream
Utilities SequenceInputStream

Источник

Java readers and streams

  • Introduction to Java
  • The complete History of Java Programming Language
  • C++ vs Java vs Python
  • How to Download and Install Java for 64 bit machine?
  • Setting up the environment in Java
  • How to Download and Install Eclipse on Windows?
  • JDK in Java
  • How JVM Works – JVM Architecture?
  • Differences between JDK, JRE and JVM
  • Just In Time Compiler
  • Difference between JIT and JVM in Java
  • Difference between Byte Code and Machine Code
  • How is Java platform independent?
  • Decision Making in Java (if, if-else, switch, break, continue, jump)
  • Java if statement with Examples
  • Java if-else
  • Java if-else-if ladder with Examples
  • Loops in Java
  • For Loop in Java
  • Java while loop with Examples
  • Java do-while loop with Examples
  • For-each loop in Java
  • Continue Statement in Java
  • Break statement in Java
  • Usage of Break keyword in Java
  • return keyword in Java
  • Object Oriented Programming (OOPs) Concept in Java
  • Why Java is not a purely Object-Oriented Language?
  • Classes and Objects in Java
  • Naming Conventions in Java
  • Java Methods
  • Access Modifiers in Java
  • Java Constructors
  • Four Main Object Oriented Programming Concepts of Java
  • Inheritance in Java
  • Abstraction in Java
  • Encapsulation in Java
  • Polymorphism in Java
  • Interfaces in Java
  • ‘this’ reference in Java

Источник

Оцените статью