Docsity

Java Streams & File I/O: Byte, Character, Data, Print, Buffered, Object, Random Access, Slides of Computer Science

Partial preview of the text

Download Java Streams & File I/O: Byte, Character, Data, Print, Buffered, Object, Random Access and more Slides Computer Science in PDF only on Docsity!

Chapter 15: Input and Output

 Stream Classes

 Processing External Files

 Data Streams

 Print Streams

 Buffered Streams

 Use JFileChooser

 Text Input and Output on the Console

 Random Access Files

 Parsing Text Files

Streams

  • A stream is an abstraction of the continuous one- way flow of data.

Program

Output Stream

File

Input Stream

Byte Stream Classes

InputStream

OutputStream

RandomAccessFile

Object

PipeOutputStream

SequenceInputStream

StringBufferInputStream ByteArrayOutputStream

ObjectOutputStream

FilterOutputStream

FileOutputStream

PipedInputStream

PushBackInputStream

BufferedInputStream LineNumberInputStream

DataInputStream

BufferedOutputStream DataOutputStream PrintStream

ObjectInputStream

FilterInputStream

FileInputStream

ByteArrayInputStream (^) InputData

OutputData

ObjectOutput

ObjectInput

Character Stream Classes

Reader

Writer

StreamTokenizer

Object

PrintWriter

BufferedWriter CharArrayWriter

PipedWriter

FilterWriter

PipedReader LineNumberReader

FileReader PushBackReader

FileWriter

StringWriter

StringReader

InputStreamReader

CharArrayReader

BufferedReader

FilterReader

OutputStreamWriter

Reader

The Reader class is similar to the InputStream class. The methods in Reader are subject to character interpretation.

abstract int read() throws IOException int read(char b[]) throws IOException void close() throws IOException long skip(long n) throws IOException

Note that if no data are available, it blocks the thread from executing next statement.

OutputStream

  • abstract void write(int b) throws IOException
  • void write(byte[] b) throws IOException
  • void close() throws IOException
  • void flush() throws IOException

Processing External Files

  • You must use file streams to read from or write to a disk file.
  • You can use FileInputStream or FileOutputStream for byte streams, and you can use FileReader or FileWriter for character streams.

File I/O Stream Constructors

Constructing instances of FileInputStream, FileOutputStream, FileReader, and FileWriter from file names:

FileInputStream infile = new FileInputStream("in.dat");

FileOutputStream outfile = new FileOutputStream("out.dat");

FileReader infile = new FileReader("in.dat");

FileWriter outfile = new FileWriter("out.dat"); Docsity.com

Data Streams

The data streams ( DataInputStream and DataOutputStream) read and write Java primitive types in a machine-independent fashion, which enables you to write a data file in one machine and read it on another machine that has a different operating system or file structure.

DataInputStream Methods

  • int readByte() throws IOException
  • int readShort() throws IOException
  • int readInt() throws IOException
  • int readLong() throws IOException
  • float readFloat() throws IOException
  • double readDouble() throws IOException
  • char readChar() throws IOException
  • boolean readBoolean() throws IOException
  • String readUTF() throws IOException

Data I/O Stream Constructors

  • DataInputStream infile = new DataInputStream(new FileInputStream("in.dat")); Creates an input file for in.dat.
  • DataOutputStream outfile = new DataOutputStream(new FileOutputStream("out.dat")); Creates an output file for out.dat.

Example 15.

Using Data Streams

TestDataStreams

Run

Click the Run button to access the DOS prompt; then type java TestDataStreams and press Enter. (Note: You cannot run this from the CD; the program writes to disk.)

DataInputStream dis

program mytemp.dat

mytemp.dat

DataOutputStream dos

FileInputStream

FileOutputStream

PrintWriter Constructors

  • PrintWriter(Writer out)
  • PrintWriter(Writer out, boolean autoFlush)
  • PrintWriter(OutputStream out)
  • PrintWriter(OutputStream out, boolean autoFlush)

PrintWriter Methods

  • void print(Object o)
  • void print(String s)
  • void println(String s)
  • void print(char c)
  • void print(char[] cArray)
  • void print(int i)
  • void print(long l)
  • void print(float f)
  • void print(double d)
  • void print(boolean b)