The Best One
The Best One
Programming-3
Lecture 04
Dr. Mohammad Ahmad
Overview
• Java I/O
– The java.io package
– Streams
• Files
– Working with Files
Java I/O – The Basics
• Java I/O is based around the concept of a stream
– Ordered sequence of information (bytes) coming from a
source, or going to a ‘sink’
– Simplest stream reads/writes only a single byte, or an
array of bytes at a time
• Designed to be platform-independent
• The stream concept is very generic
– Can be applied to many different types of I/O
– Files, Network, Memory, Processes, etc
Streams
• Stream: an object that either delivers data to its destination (screen,
file, etc.) or that takes data from a source (keyboard, file, etc.)
– it acts as a buffer between the data source and destination
• Input stream: a stream that provides input to a program
– System.in is an input stream
• Output stream: a stream that accepts output from a program
– System.out is an output stream
• A stream connects a program to an I/O object
– System.out connects a program to the screen
– System.in connects a program to the keyboard
Binary Versus Text Files
• All data and programs are ultimately just zeros and ones
– each digit can have one of two values, hence binary
– bit is one binary digit
– byte is a group of eight bits
• Text files: the bits represent printable characters
– one byte per character for ASCII, the most common code
– for example, Java source files are text files
– so is any file created with a "text editor"
• Binary files: the bits represent other types of encoded information, such as
executable instructions or numeric data
– these files are easily read by the computer but not humans
– they are not "printable" files
• actually, you can print them, but they will be unintelligible
• "printable" means "easily readable by humans when printed"
Java I/O – The Basics
• The java.io package contains all of the I/O
classes.
– Many classes specialised for particular kinds of stream
operations, e.g. file I/O
• Reading/writing single bytes is quite limited
– So, it includes classes which provide extra functionality
– e.g. buffering, reading numbers and Strings (not bytes),
etc.
• Results in large inheritance hierarchy, with
separate trees for input and output stream classes
Java I/O -- InputStream
Java I/O – InputStreams
Java I/O – Using InputStreams
• Basic pattern for I/O programming is as
follows:
Open a stream
While there’s data to read
Process the data
Close the stream
Java I/O – Using InputStreams
• I/O in Java:
InputStream in = new FileInputStream(“c:\\temp\\
myfile.txt”);
int b = in.read();
//EOF is signalled by read() returning -1
while (b != -1)
{
//do something…
b = in.read();
}
in.close();
Java I/O – Using InputStreams
• But using buffering is more efficient, therefore we
always nest our streams…
InputStream inner = new FileInputStream(“c:\\temp\\
myfile.txt”);
InputStream in = new BufferedInputStream(inner);
int b = in.read();
//EOF is signalled by read() returning -1
while (b != -1)
{
//do something…
b = in.read();
}
in.close();
Java I/O – Using InputStreams
• We’ve omitted exception handling in the
previous examples
• Almost all methods on the I/O classes
(including constructors) can throw an
IOException or a subclass.
• Always wrap I/O code in try…catch blocks
to handle errors.
Java I/O – Using InputStreams
InputStream in = null;
try
{
InputStream inner = new FileInputStream(“c:\\temp\\
myfile.txt”);
in = new BufferedInputStream(inner);
//process file
} catch (IOException e)
{
e.printStackTrace();
}
finally
{
try { in.close(); } catch (Exception e) {}
}
Java I/O – OutputStream
Java I/O – OutputStreams
Java I/O – Using InputStreams
• Basic pattern for output is as follows:
Open a stream
While there’s data to write
Write the data
Close the stream
Java I/O – Using
OutputStreams
• Output in Java:
OutputStream out = new FileOutputStream(“c:\\temp\\
myfile.txt”);
while (…)
{
out.write(…);
}
out.close();
Java I/O – Using
OutputStreams
OutputStream out = null;
try
{
OutputStream inner = new FileOutputStream(“c:\\temp\\
myfile.txt”);
out = new BufferedOutputStream(inner);
//process file
} catch (IOException e)
{
e.printStackTrace();
}
finally
{
try { in.close(); } catch (Exception e) {}
}
Java I/O – Writer
Java I/O – Writers
Using Writers
Writer out = null;
try
{
Writer inner = new FileWriter(“c:\\temp\\myfile.txt”);
out = new BufferedWriter(inner);
PrintWriter FileOutputStream
Memory Disk
smileyOutStream smiley.txt
• print
• format
• flush: write buffered output to disk
• close: close the PrintWriter stream (and file)
Bridging the Gap
• Sometimes you need to bridge across the two
hierachies
– Use InputStreamReader or OutputStreamWriter
• InputStreamReader
– Reads bytes from an InputStream, and turns them into
characters using a character encoding
• OutputStreamWriter
– Turns characters sent to the Writer into bytes written by
the OutputStream, again using a character encoding.
The File Object
• Java provides access to the file system through the
java.io.File object
– Represents files and directories
• Has methods for working with files and directories
– Making directories, listing directory contents
– renaming and deleting, checking permissions, etc
• Check whether the File corresponds to a directory or a
file with isDirectory()
• Well-featured, and intuitive
– Take a look through the javadocs
• Quick example…
File Class [java.io]
• Acts like a wrapper class for file names
• A file name like "numbers.txt" has only String properties
• File has some very useful methods
– exists: tests if a file already exists
– canRead: tests if the OS will let you read a file
– canWrite: tests if the OS will let you write to a file
– delete: deletes the file, returns true if successful
– length: returns the number of bytes in the file
– getName: returns file name, excluding the preceding path
– getPath: returns the path name—the full name
-------------------
Scanner inFile = …
while (inFile.hasNextLine())
{
line = infile.nextLine();
// …
}
BufferedReader inFile = …
line = inFile.readline();
while (line != null)
{
// …
line = inFile.readline();
}
-------------------
BufferedReader inFile = …
while ((line = inFile.readline()) != null)
{
// …
}
My suggestion
• Use Scanner with File
– new Scanner(new File(“in.txt”))
• Use hasNext…() to check for EOF
– while (inFile.hasNext…())
• Use next…() to read
– inFile.next…()
• Simpler and you are familiar with
methods for Scanner
My suggestion cont…
• File input
– Scanner inFile =
new Scanner(new File(“in.txt”));
• File output
– PrintWriter outFile =
new PrintWriter(new File(“out.txt”));
– outFile.print(), println(),
format(), flush(), close(), …