CSE 501N Fall ‘06 02: Machine Overview + Fundamental Types

Download Report

Transcript CSE 501N Fall ‘06 02: Machine Overview + Fundamental Types

CSE 501N
Fall ‘09
18: Files and Streams
06 November 2009
Nick Leidenfrost
Lecture Outline

Storing data to the hard disk
 Files
 Streams
 Storage Decisions
 Serialization
2
File System
File Hierarchy

In general, files in computers
are organized in a directory
tree
 A directory
is a virtual container
that holds files and other
directories
 A.k.a folder
3
Files
Naming Conventions
“full path”
Windows
extension
H:/workspace/Lab6/Ship.java
drive
path
filename
Mac / Linux / Unix
/home/username/workspace/Lab6/Ship.java


Filenames are unique and case-sensitive
Extensions serve as a “hint” or “shortcut” for the
type of data contained in the file
 For
Us
 For the Operating System (OS)
4
Referring to Files in Programs
Absolute vs. Relative Paths

Absolute Path
 The
“full path” to the file
H:/workspace/Lab6/images/mothership.gif

Relative Path
 A path
that specifies a file’s location relative to
another location

“another location” = the location of our program
images/Ship.java
5
Relative Paths
./ And ../
Some special notation specific to relative
paths lets us refer to our own directory, as
well as our parent directory
 ./ and ../

 ./
= The current directory
 ../ = The parent directory of the current
directory (or the directory above the current
directory)

This notation is fairly standard in
computing
[ cmd example ]
6
Relative Paths
Examples

Relative Paths
 Inside
our directory
mothership.gif
./mothership.gif
 Inside a subdirectory
images/mothership.gif
images/gif/small/mothership.gif
 In
our “parent” directory
../motherhsip.gif
 In a “sibling directory”
../images/motherhsip.gif
 In directory above our parent directory
../../../motherhsip.gif
7
Absolute vs. Relative

Absolute
 Path
will rely on exactly the same directory structure
being in place on every computer
 Application can be moved independently of
resources, and resources will still be found
 Generally not portable (System-specific)
 Usually set on installation

Relative
 Will
be correct as long as resources stay in the same
place relative to the application
 Application and resources can be moved and still
function


From computer to computer
From directory to directory on the same computer
8
File Formats
Text and Binary

Two ways to store data:
 Text format (a.k.a. plain-text)
 Data stored as characters
 Human readable
 Less efficient with respect to storage
 A.k.a. ASCII (ask · ee)

(American Standard Code For Information Interchange)
 Binary format
 Data stored as bytes
 Looks like gibberish to Humans
 Relies on a defined structure
 More compact / efficient than plain-text

// Let’s look at some examples!
9
ASCII Format

Let’s look at storing an integer in a plaintext file
int in Java is 4 bytes
 We want to store the number 12,345
 Our file actually holds the character ‘1’,
followed by ‘2’, then ‘3’, ‘4’ and finally, ‘5’
 An

This takes at least 5 bytes
10
Binary Format
Data items are represented in bytes
 Integer 12,345 stored as a sequence of
four bytes:

0 0 48 57
 48*256¹ + 57*256º
 Why the zeros? Why not just use 2 bytes to
store it?


More compact and more efficient
11
Files in Java
Library Support

Support for file interaction (and more) can be
found in Java’s java.io library
 “io”:
Input / Output

A file is represented in the library by the File
class in java.io.File

We can create file objects with either absolute or
relative paths
// Let’s have a look at java.io.File

12
Reading Text Files



Simplest way to read text: use Scanner class
To read from a file on disk, construct a FileReader
Then, use the FileReader to construct a Scanner
object
FileReader reader = new FileReader("input.txt“);
Scanner in = new Scanner(reader);

Use the Scanner methods to read data from file
 next, nextLine, nextInt, and nextDouble

// Let’s look at the Scanner API
13
Writing Text Files

To write to a file, construct a
PrintWriter object
 //
Let’s look at the PrintWriter API
PrintWriter out = new PrintWriter("output.txt");
If file already exists, it is emptied before
the new data are written into it
 If file doesn't exist, an empty file is created

14
Writing Text Files

Use print and println to write into a
PrintWriter:
out.println(29.95);
out.println(new Rectangle(5, 10, 15, 25));
out.println("Hello, World!");

You must close a file when you are done
processing it:
out.close();

Otherwise, not all of the output may be written to
the disk file
15
A Sample Program
Reads all lines of a file and sends them to
the output file, preceded by line numbers
 Sample input file:

Mary had a little lamb
Whose fleece was white as snow.
And everywhere that Mary went,
The lamb was sure to go!
16
A Sample Program

Program produces the output file:
/*
/*
/*
/*

1
2
3
4
*/
*/
*/
*/
Mary had a little lamb
Whose fleece was white as snow.
And everywhere that Mary went,
The lamb was sure to go!
(Program could be used for numbering
Java source files, etc.)
17
Write the code for this program

// Code Example: FileNumberer.java
18
File Dialog Boxes

More user friendly way of selecting files

// Let’s integrate this into our FileNumberer
22
Text Format




Human-readable form
Data stored as sequence of characters
 Integer 12345 stored as characters '1' '2' '3' '4' '5'
Use Reader and Writer and their subclasses to
process input and output
To read:
FileReader reader = new FileReader("input.txt");

To write
FileWriter writer = new FileWriter("output.txt");
24
Binary Format

Reading and writing binary files
subclasses of InputStream and
OutputStream
 Use

To read:
FileInputStream inputStream
= new FileInputStream("input.bin");

To write
FileOutputStream outputStream
= new FileOutputStream("output.bin");
25
Streams: Input and Output

We read from an
InputStream

We write to an
OutputStream
 As
with System.out
 Imagine
Output
(writing)
The stream
the “Stream” as a
hose connecting the data
source and the data
destination
Input
(reading)
26
Reading a Single Character from a
File in Text Format

Use various read methods in
InputStream class to read a single byte
/ array of bytes
 returns

 or
the next byte as an int
Returned value 0 <= x <= 255
the integer -1 at end of file
InputStream in = . . .;
int next = in.read();
byte b;
if (next != -1)
b = (byte) next;
27
Text and Binary Format



Use variations of the write method to write a
single byte / array of bytes
read and write are the only input and output
methods provided by the file input and output
classes
Java stream package principle: each class
should have a very focused responsibility
 Use
Library of subclasses for more high-level
behavior
28
Text and Binary Format
Job of InputStream / OutputStream:
interact with data sources and get bytes
 To read numbers, strings, or other objects,
combine a Stream with other classes

 E.g.
java.util.Scanner
29
File Example
A Simple Encryption Program

File encryption
 To
scramble it so that it is readable only to
those who know the encryption method and
secret keyword

To use Caesar cipher
 Choose
an encryption key–a number between
1 and 25
 Example: If the key is 3, replace A with D, B
with E, . . .
30
An Encryption Program

Example text:

To decrypt, use the negative of the
encryption key
31
Code for this?
// Code Example: FileEncryptor.java
32
Storage Options
Random Access vs. Sequential Access

Sequential access
 A file
is processed a byte at a time
 It can be inefficient

Random access
 Allows
access at arbitrary locations in the file
 Only files on disk support random access

System.in and System.out (normal input and
output streams) do not
 Each

disk file has a special file pointer position
You can read or write at the position where the file
pointer is
38
Storage Options
Random Access vs. Sequential Access

Each disk file has a special file pointer
position
 You
can read or write at the position where
the pointer is
39
RandomAccessFile

You can open a file either for
 Reading
only ("r")
 Reading and writing ("rw")
RandomAccessFile f = new RandomAcessFile("bank.dat","rw");

To move the file pointer to a specific byte
f.seek(n);

(moves file pointer to nth byte)
40
RandomAccessFile

To get the current position of the file
pointer:
long n = f.getFilePointer();
// of type "long" because files can be very large

To find the number of bytes in a file:
long fileLength = f.length();
41
Random Access
A Sample Program
Use a random access file to store a set of
bank accounts
 Program lets you pick an account and
deposit money into it
 To manipulate a data set in a file, pay
special attention to data formatting

 Suppose
we store the data as text
Say account 1001 has a balance of $900, and
account 1015 has a balance of 0
42
Random Access
A Sample Program
What if we want to deposit $100 into account
1001?
If we now simply write out the new value, the result
is
43
Random Access
A Sample Program

What if money becomes too big?
 This
is caused by one of the downsides of humanreadable file formats

Better way to manipulate a data set in a
file:
 Give
each value a fixed size that is sufficiently large
 Every record has the same size
 Easy to skip quickly to a given record
 To store numbers, use binary format for scalability
44
Random Access
A Sample Program
RandomAccessFile class stores binary
data
 readInt and writeInt read/write
integers as four-byte quantities
 readDouble and writeDouble use 8
bytes

double x = f.readDouble();
f.writeDouble(x);
45
Random Access
A Sample Program

To find out how many bank accounts are in
the file
public int numAccounts () throws IOException {
return (int) (file.length() / RECORD_SIZE);
// RECORD_SIZE is 12 bytes:
// 4 bytes for the account number and
// 8 bytes for the balance
}
46
Random Access
A Sample Program

To read the nth account in the file
public BankAccount read (int n) throws IOException {
file.seek(n * RECORD_SIZE);
int accountNumber = file.readInt();
double balance = file.readDouble();
return new BankAccount(accountNumber, balance);
}
47
Random Access
A Sample Program

To write the nth account in the file
public void writeNth (int n, BankAccount account)
throws IOException {
file.seek(n * RECORD_SIZE);
file.writeInt(account.getAccountNumber());
file.writeDouble(account.getBalance());
}
48
Object Streams
Reading and Writing Objects? WTF?





Writing Objects directly to streams
ObjectOutputStream class can write a
entire objects to disk
ObjectInputStream class can read
objects back in from disk
Objects are saved in binary format; hence, you
use streams
// Let’s look at the APIs
49
Writing a BankAccount Object
to a File

The object output stream saves all
instance variables
BankAccount b = . . .;
OutputStream os = new FileOutputStream("bank.dat");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(b);
50
Reading a BankAccount Object
From a File
readObject returns an Object
reference
 Hence, we must remember the types of
the objects that you saved and use a cast

InputStream is = new FileInputStream("bank.dat");
ObjectInputStream in = new ObjectInputStream(is);
BankAccount b = (BankAccount) in.readObject();
51
Reading a BankAccount Object
From a File

readObject method can throw a
ClassNotFoundException
 Why


is this?
It is a checked exception
You must catch or declare it
52
Writing Complex Objects
Write and Read an ArrayList to a File

Write
ArrayList<BankAccount> bl = new ArrayList<BankAccount>();
// Now add many BankAccount objects into bl
out.writeObject(bl);

Read
ArrayList<BankAccount> bl = (ArrayList<BankAccount>)
in.readObject();
53
Serializable

Objects that are written to an object
stream must belong to a class that
implements the Serializable interface.
class BankAccount implements Serializable {
. . .
}

Serializable interface has no
methods.
 What
is it good for then!?
54
Serializable

Implementing Serializable tells Java
that a class can be serialized
 Most
issues of serialization cannot be:
Entirely identified with an interface
 Detected by the compiler

 Therefore
Java is forced to trust us
The interface has only Semantic meaning
 Exceptions will be thrown if problems arise


If you want more control over serialization,
implement java.io.Externizable
55
Serializable

Serialization: process of saving objects to
a stream
 Each
object is assigned a serial number on
the stream
 If the same object is saved twice, only serial
number is written out the second time
 When reading, duplicate serial numbers are
restored as references to the same object
56
Conclusion
Questions?
 I will be in Lab now until 6:30

57