CIS 234: More File Input & Output

Download Report

Transcript CIS 234: More File Input & Output

CIS 234:
More File Input & Output
Dr. Ralph D. Westfall
May, 2007
Formatted File Data


when do we need to know how many
bytes are in different variable types?
file data is a stream of bytes, which can
be in different formats, including


char (2 bytes per character in Unicode)
non-character data e.g., int (4 bytes),
double (8 bytes), boolean (1 byte)
Formatted File Data - 2

Java has classes for handling different
types of (non-character) formatted data



DataInputStream for reading
DataOutputStream for writing
can create members of these classes by
passing File[Input or Output]Stream
objects to the constructors

also known as "chaining"
Formatted File Data - 3
creating output and input streams
DataOutputStream out; // declare object
out = new DataOutputStream (new
FileOutputStream("myfile.txt"));
// 2nd object created as argument to 1st
DataInputStream inputs;
inputs = new DataInputStream (new
FileInputStream("myfile.txt"))

Writing Formatted Data
out.writeChar('c'); //writes char c as int
//A = 65, a = 97, etc.
out.writeChars(str); // uses size of String
//writes String characters as ints
out.writeUTF(str); //writes String str
// 1st 2 bytes = length of String
Writing Formatted Data - 2
out.write(7); //writes an int (4 bytes)
out.writeDouble(1.0); //double (8 bytes)
out.writeLong(796342L); //long (8 bytes)
out.writeBoolean(true);//boolean (1 byte)
// usually write variables, not literals
// why is number of bytes important?
Reading Formatted Data
inputs.readChar(); //reads a char as int
//no readChars method
//why not? (see below)
String name = inputs.readUTF();
//reads length of String, and then reads
//that number of characters into a String
Reading Formatted Data - 2
byte w[] = new byte[1]; //array of bytes
w=inputs.read();
// reads 1 byte
int x=inputs.readInt(); // reads 4 bytes
double y=inputs.readDouble();
// reads 8 bytes
boolean z=inputs.readBoolean();
// reads 1 byte
Sequential vs. Random Access


sequential access means read or write
from start of file, one byte at a time
random access means starting
anywhere in file



at start, at end, or anywhere in between
random access also known as direct access
makes it possible to change part of data
without writing the whole file again
Random Access

Java works with streams rather than
records, so access is based on bytes
rather than records


if each "record" has an identifiable number
of bytes, can handle as records
RandomAccessFile class can be either
r (read only) or rw (read and write)

based on argument when opened
Random Access - 2
use seek() method to go to location
where need to read or write
int c;
RandomAccessFile inFile = new
RandomAccessFile("datafile.txt", "r");
// read only
inFile.seek(5); // count starts at 0
c = inFile.readChar(); // reads 6th char

Review



How many bytes are in a file for each
character? int? double?
You also need to know the ____ to read
diverse types of data from a file.
what doe Java provide to help write
programs that read or write data?
Review - 2

what does sequential access mean?


give an example of sequential access?
what does random access mean?

what is the advantage of random access?