0% found this document useful (0 votes)
44 views

To Programming: COMP1681 / SE15

The document discusses streams and file input/output in Java. It introduces streams as flows of data into and out of a program. It covers the differences between text and binary files, and how to open, write to, read from, and close both text input and output files using classes like PrintWriter, BufferedReader, and File. It also discusses exceptions that can occur during file I/O and using path names to specify file locations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

To Programming: COMP1681 / SE15

The document discusses streams and file input/output in Java. It introduces streams as flows of data into and out of a program. It covers the differences between text and binary files, and how to open, write to, read from, and close both text input and output files using classes like PrintWriter, BufferedReader, and File. It also discusses exceptions that can occur during file I/O and using path names to specify file locations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

COMP1681 / SE15

Today’s Learning Objectives


Introduction
 To become familiar with the concept of an I/O stream.
to Programming  Understand the difference between binary and text files.
 Learn how to save data in a file.
 Learn how to read data from a file.
Lecture 33
Streams and File I/O

SE15: Streams and File I/O 30–2

Lecture Outline What is a Stream?


 A stream is a flow of data
 Overview of Streams and File I/O  characters, numbers or bytes of binary digits
 Text file Output
 Text File Input  Input Stream – data flows into your program (files,
keyboard)
 Output Stream data flows out (Screen, files)
Savitch Chapter 9
 Java implements streams as objects of stream classes

 Examples we have seen so far?


SE15: Streams and File I/O 30–3 SE15: Streams and File I/O 30–4

Text files vs Binary files Text File I/O


 All data in any file are stored as binary digits (0’s & 1’s)
 3 steps are required
 Text files  1. Open a file (connects a file to a stream)
 Can think of a file's contents as consisting of a sequence of
characters.
 2. Write to the file
 Java streams provide methods that make the binary digits look  3. Close the file
like characters to your program or editor.
 Usually appear the same on all computers, because they use
common standards (such as ASCII or Unicode)  Classes we shall look at:
 Human readable.
 PrintWriter class
 Binary files
 BufferedReader class
 Contents are handled as a sequences of binary digits.  File class
 More efficient.
 Implementation differs between computers.

SE15: Streams and File I/O 30–5 SE15: Streams and File I/O 30–6
Opening a text file for writing with
PrintWriter FileNotFoundException
 First declare a variable of type PrintWriter e.g..  When you open a file for writing the constructor might throw an exception
PrintWriter outputStream = null; FileNotFoundException
 It means the file could not be created, such as when the filename was
(this is the name of the stream) already in use for a directory name
 You should therefore catch the exception in a catch block or throw it
 Next construct the outputStream object by calling the constructor for PrintWriter outputStream = null;
the PrintWriter class try
 The PrintWriter class has no constructor that takes a file {
name as its argument so we use the class FileOutputStream to outputStream = new PrintWriter(new
create a stream that can be used as an argument to the FileOutputStream(“out.txt”));
PrintWriter constructor e.g. }
new FileOutputStream(“out.txt”) catch(FileNotFoundException e)
{
 outputStream = new PrintWriter( System.out.println(“Error openning file”);
System.exit(0);
new FileOutputStream(“out.txt”));
}

SE15: Streams and File I/O 30–7 SE15: Streams and File I/O 30–8

Writing to a text file with


PrintWriter Closing a file with PrintWriter
 When your program has finished writing to a file, it
 PrintWriter has a method println that behaves like should close the stream connected to the file.
the System.out.println method but the output goes outputStream.close();
to a text file
 outputStream.println(“this line will be  Why bother closing a file?
written to a file”);  Java will close any open files for you if your program
terminates normally.
 System.out.println(“this line will be  If an error occurs the files may not be closed and the files
displayed on the screen”); can be damaged.
 If you open a file for writing you must close it before it can
be read.
SE15: Streams and File I/O 30–9 SE15: Streams and File I/O 30–10

Opening a text file for input with


Overwriting and appending files BufferedReader
 Connecting to a stream using a) declare a stream variable e.g.
outputStream = new PrintWriter(new BufferedReader inputStream = null;
FileOutputStream(“out.txt”));
Will always produce an empty file even if a file of that b) construct the stream object
name already exists!! As with PrintWriter there is no constructor that takes a
file name as an argument therefore we need another class
FileReader to help with opening the file.
 If you wish to add data to a file (appending a file) you
must use inputStream = new BufferedReader( new
 outputStream = new PrintWriter(new FileReader(“data.txt”));
FileOutputStream(“out.txt”,true));
You should also catch the FileNotFoundException.
SE15: Streams and File I/O 30–11 SE15: Streams and File I/O 30–12
Reading data from the file with
BufferedReader Using path names
 The method readLine reads from a text file just as the nextLine  A typical UNIX path name is
method of the class Scanner reads a line of text from the
keyboard. /user/smith/homework1/data.txt
String line = inputStream.readLine();  To create a FileReader for this file you use
new FileReader(“/user/smith/homework1/data.txt”)
 It does not have methods such as nextInt and nextDouble.

 readLine may throw an IOException


 Windows uses \ instead of / in path names

 When readline tries to read beyond the end of the file it returns  A typical Windows path name is
null D:\homework1\data.txt
 When your program has finished reading from the file, it should
 To create a FileReader for this file you use
close the stream connected to the file. new FileReader(“D:\\homework1\\data.txt”);
inputStream.close();
SE15: Streams and File I/O 30–13 SE15: Streams and File I/O 30–14

Summary Follow-up Work


 Looked at the concept of Streams  Savitch Chapter 9
 Learnt how to write text files using the PrintWriter  For next time
class  File class
 Learnt how to read text files using the Buffered Reader  Binary File I/O
class

SE15: Streams and File I/O 30–15 SE15: Streams and File I/O 30–16

You might also like