wpjr2’s Weblog

Artigos e tutoriais úteis

Resumo dos Fluxos de Dados em Java

Escrito por wpjr2 em Maio 3, 2008

How to choose/use Streams

Choosing IO Streams

(1) Memory IO
Array : create these streams on an existing array and then use the read and write methods to read from or write to the array.

CharArrayReader, CharArrayWriter
ByteArrayInputStream, ByteArrayOutputStream

String : to read/write characters from/to a String in memory.

StringReader, StringWriter
StringBufferInputStream

(2) Pipe IO

Pipe : Pipes are used to channel the output from one thread into the input of another.

PipedReader, PipedWriter
PipedInputStream, PipedOutputStream

(3) File IO

File : to read from or write to a file on the native file system.

FileReader, FileWriter
FileInputStream, FileOutputStream

(4) Concatenation

Concatenation : concatenates multiple input streams into one input stream.

N/A
SequenceInputStream

(5) Object Serialization

Object Serialization : used to serialize objects.

N/A
ObjectInputStream, ObjectOutputStream

(6) Data Conversion

Data Conversion : Read or write primitive data types in a machine-independent format.

N/A
DataInputStream, DataOutputStream

(7) Counting

Counting : keeps track of line numbers while reading.

LineNumberReader
LineNumberInputStream

( 8) Peeking Ahead

Peeking Ahead : These input streams each have a pushback buffer. When reading data from a stream, it is sometimes useful to peek at the next few bytes or characters in the stream to decide what to do next.

PushbackReader
PushbackInputStream

(9) Printing

Printing : contain convenient printing methods.

PrintWriter
PrintStream

(10) Buffering

Buffering : buffer data while reading or writing, thereby reducing the number of accesses required on the original data source.

BufferedReader, BufferedWriter
BufferedInputStream, BufferedOutputStream

(11) Filtering

Filtering : These abstract classes define the interface for filter streams, which filter data as it’s being read or written.

FilterReader, FilterWriter
FilterInputStream, FilterOutputStream

(12) Converting between Bytes and Characters

Converting : A reader and writer pair that forms the bridge between byte streams and character streams.

InputStreamReader, OutputStreamWriter

File Streams

The file streams– FileReader, FileWriter, FileInputStream, and FileOutputStream– read or write from/to a file on the native file system.

1.Create File Object - optional
2.Create File Stream Object using File Obejct or String file name
3.read from / write to the stream
4.close the stream

File f = new File(”data.txt”)
FileReader fr = new FileReader(f)
int c = fr.read();
close(fr);

File f = new File(”data.txt”)
FileWriter fw = new FileWriter(f)
fw.write(c);
close(fw);

Pipe Streams

Pipes are used to channel the output from one thread into the input of another. PipedReader and PipedWriter (PipedInputStream and PipedOutputStream) implement the input and output components of a pipe.
A piped input stream should be connected to a piped output stream. Typically, data is read from a PipedInputStream(PipedReader) object by one thread and data is written to the corresponding PipedOutputStream(PipedWriter) by some other thread. Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread.

To make Pipe Connections, Use constructor or connect method. There are 4 ways to connects pipe streams.

PipedWriter pipeOut = new PipedWriter();
PipedReader pipeIn = new PipedReader(pipeOut);

PipedReader pipeIn = new PipedReader();
PipedWriter pipeOut = new PipedWriter(pipeIn);

PipedWriter pipeOut = new PipedWriter();
PipedReader pipeIn = new PipedReader();
pipeOut.connect(pipeIn);

PipedWriter pipeOut = new PipedWriter();
PipedReader pipeIn = new PipedReader();
pipeIn.connect(pipeOut);

Concatenation

The SequenceInputStream creates a single input stream from multiple input sources. It represents the logical concatenation of other input streams,

SequenceInputStream has 2 kinds of constructors.

public SequenceInputStream(Enumeration e)
publicSequenceInputStream(InputStream s1, InputStream s2)

Example - Concatenating Files

1.Make the list of files to be concatenated using Enumeration.
2.Creating the SequenceInputStream
3.Read data from the SequenceInputStream
4.Write them to the file

Deixe um comentário

XHTML: Você pode usar estas tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>