0% found this document useful (0 votes)
55 views17 pages

I/O and Parsing: Reading and Writing With Java's Input/Output Streams and Parsing Utilities

This document discusses Java input/output (I/O) streams and parsing utilities. It covers the basics of I/O, including reading from and writing to sources like files and network connections. It describes Java's I/O stream classes for byte and character streams, and how to use Reader and Writer subclasses like FileReader, FileWriter, and BufferedReader to read from and write to text files. It also provides an example of copying text files using I/O streams. Finally, it discusses reading from keyboard input using System.in and BufferedReader.

Uploaded by

Minalew Guche
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views17 pages

I/O and Parsing: Reading and Writing With Java's Input/Output Streams and Parsing Utilities

This document discusses Java input/output (I/O) streams and parsing utilities. It covers the basics of I/O, including reading from and writing to sources like files and network connections. It describes Java's I/O stream classes for byte and character streams, and how to use Reader and Writer subclasses like FileReader, FileWriter, and BufferedReader to read from and write to text files. It also provides an example of copying text files using I/O streams. Finally, it discusses reading from keyboard input using System.in and BufferedReader.

Uploaded by

Minalew Guche
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lecture 15

I/O and Parsing

Reading and Writing with


Java's Input/Output Streams
and Parsing Utilities
Input/Output Basics
 Input/Output = I/O = communication
between a computer program and external
sources and destinations of information
 Involves Reading and Writing
 Reading input from a source
 Writing output to a destination
 Example Sources and Destinations:
 Files
 Network connections
 Other programs
Java I/O Streams
 Java uses an I/O system called
streams (pioneered in C++)

 Java provides java.io package to


implement streams

 Streams treat all external source and


destinations of data the same way:
as "streams" of information
Input vs. Output Streams
 Reading from an Input Stream

 Writing to an Output Stream


Byte vs. Character Streams
 Byte Streams are used to read and write
data in binary format (1's and 0's)
example data: images, sounds, executable
programs, word-processing documents, etc.

 Character Streams are used to read and


write data in text format (characters)
example data: plain text files (txt extension),
web pages, user keyboard input, etc.
Java Classes
 Package java.io offers classes to connect to
streams

 To connect to a stream, instantiate a subclass of


one of these abstract superclasses:

input output
byte InputStream OutputStream
character Reader Writer
Using a Stream Class

1. Open a stream by instantiating a


new stream object

2. While more information to read/write,


read/write that data

3. Close the stream by calling the


object’s close() method
Using a Reader
 Recall: a Reader is used to read a
character input stream

 Reader offers these methods to read single


characters and arrays of characters:
int read()
int read(char cbuf[])
int read(char cbuf[], int offset, int length)

 Reader is abstract so you must instantiate


a subclass of it to use these methods
How to Read from a Text File
public void readFile() {
FileReader fileReader = null;
try {
fileReader = new FileReader("input.txt");
int c = fileReader.read();
while (c != -1) {
// cast c to char and use it
c = fileReader.read();
}
} catch (FileNotFoundException e) {
System.out.println("File was not found");
} catch (IOException e) {
System.out.println("Error reading from file");
} finally {
if (fileReader != null) {
try { fileReader.close(); }
catch (IOException e) { /* ignore */ }
}
}
}
Wrap in a BufferedReader
 BufferedReader has a readLine() method to
read an entire line of characters efficiently

 Wrap a Reader with a BufferedReader by


passing the Reader as a constructor argument
FileReader fr = new FileReader("myFile.txt");
BufferedReader br = new BufferedReader(fr);

 The readLine() method returns null when there


are no more lines to read
Using BufferedReader
public void readFileWithBufferedReader() {
BufferedReader bufferedReader = null;
try {
FileReader fr = new FileReader("input.txt");
bufferedReader = new BufferedReader(fr);
String line = bufferedReader.readLine();
while (line != null) {
// do something with line
line = bufferedReader.readLine();
}
} catch (FileNotFoundException e) {
System.out.println("File was not found");
} catch (IOException e) {
System.out.println("Error reading from file");
}
if (bufferedReader != null) {
try { bufferedReader.close(); }
catch (IOException e) { /* ignore */ }
}
}
Writers
 Writer is an abstract class to write to
character streams

 Offers write methods to write single


characters, arrays of characters, and strings
void write(int c)
void write(char cbuf[])
void write(String str)

 BufferedWriter offers efficient writing and a


newLine() method to insert a blank line

 Close writers with close() method when done


How to Write to a Text File
public void writeFileWithBufferedWriter() {
BufferedWriter buffWriter = null;
try {
FileWriter fw = new FileWriter("output.txt");
buffWriter = new BufferedWriter(fw);
while (/*still stuff to write */) {
String line = // get line to write
buffWriter.write(line);
buffWriter.newLine();
}
} catch (IOException e) {
System.out.println("Error writing to file");
}
if (buffWriter != null) {
try { buffWriter.close(); }
catch(IOException e) { /* ignore */ }
}
}
Example: Copying Text Files
void copyFiles(String inFilename, String outFilename)
throws FileNotFoundException {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(inFilename));
bw = new BufferedWriter(new FileWriter(outFilename));
String line = br.readLine();
while(line != null) {
bw.write(line);
bw.newLine();
line = br.readLine();
}
} catch (IOException e) {
System.out.println("Error copying files");
}

if (br != null) {try {br.close();} catch(IOException e) {}}


if (bw != null) {try {bw.close();} catch(IOException e) {}}
}
Reading from Keyboard Input
 Keyboard input is sent over a stream referred
to as "standard" input
 Java "standard" input is the InputStream
object System.in (a byte stream)
 To read characters over an InputStream,
need to wrap it in an InputStreamReader
 To read line by line, wrap the Input-
StreamReader with a BufferedReader
Reading from Keyboard Input
/**
* Returns a line read from keyboard input.
* Return null if there was an error reading the line.
*/
public void String readKeyboardLine() throws IOException {
BufferedReader br = null;
String line = null;
try {
br = new BufferedReader(new InputStreamReader(System.in));
line = br.readLine();
} catch (IOException e) {}

if (br != null) {
try { br.close(); }
catch (IOException e) { /* ignore */ }
}

return line;
}
What We've Learned So Far
 Types of Streams
 Input vs. output streams
 Byte vs. character streams
 How to . . .
 Read from text files
 Write to text files
 Read text from keyboard input
 Use buffered streams
 You are left on your own to figure out
how to use other streams

You might also like