Streams Presentation

Download Report

Transcript Streams Presentation

Streams
Binary files
• A binary file contains
”raw” bytes that can be
interpreted as almost
anything
• Can have any extension
• Can be created from
various sources
RHS – SWC
2
Binary files
• Note that a binary file does
usually not contain information
about itself (metadata)
• We must know the meaning of
the file in advance
– Picture
– Object data
– Encrypted data
RHS – SWC
3
Binary files and Java
• In Java, a binary file is considered a
”stream” of bytes
• We generally use InputStream and
OutputStream to handle binary data
• For binary files, we use:
– Read: FileInputStream
– Write: FileOutputStream
RHS – SWC
4
Binary files and Java
FileInputStream inS = new FileInputStream(”input.dat”);
boolean done = false;
while (!done)
{
int next = inS.read();
byte nextByte;
if (next != -1)
{
nextByte = (byte)next;
... // Do something with nextByte
}
else {done = true; }
}
inS.close();
RHS – SWC
5
Binary files and Java
FileOutputStream outS = new
FileOutputStream(”output.dat”);
byte nextByte;
boolean done = false;
while (!done)
{
... // We get bytes to write from somewhere
outS.write(nextByte);
}
outS.close();
RHS – SWC
6
Binary files – random access
• Sequential processing of binary files is
fairly simple – but possibly inefficient
• What if a binary file contained data for
thousands of bank accounts, and we just
needed data for one…?
• Java also allows ”random access” to data
in a binary file
RHS – SWC
7
Binary files – random access
• A file also has a file pointer
• The file pointer indicates where to
read/write the next time
1
9
7
7
0
5
RHS – SWC
1
2
9
8
Binary files – random access
• Random access in a file is done using the
RandomFileAccess class
–
–
–
–
new RandomAccessFile(”data.bin”,”rw”)
f.seek(pointerPos)
int pointerPos = f.getFilePointer()
long fileLen = f.length();
RHS – SWC
9
Binary files – random access
• Random access can be efficient when
dealing with large binary files
• Again, no help from Java – you must
decide and obey a data format yourself
• If you overwrite data – too bad…
• A little bit of help – Java does offer
methods for read/write of numeric types
from a binary file
RHS – SWC
10
Binary files and Java
• Java only provides simple methods for
reading/writing binary files – what else
could it do…?
• We are responsible for interpreting and
processing the stream of bytes
• Java gets the bytes for us, it does not try
to analyse them…
RHS – SWC
11
Object streams
• Wouldn’t it be nice, if we could
– Convert an object to binary format
– Write the binary data to a file
– Close the program
– Open the program anew
– Read the binary data from the file
– Convert the data back to an object
RHS – SWC
12
Object streams
• An object stream can do just that!
• Two classes available
– ObjectOutputStream
– ObjectInputStream
• Only prerequisite; a class must implement
the Serializable interface, in order to be
writable and readable by an object stream
RHS – SWC
13
Object streams
public interface Serializable
{
// Empty...
}
RHS – SWC
14
Object streams
• Typical way to use object streams:
– Define a class where one object can contain
all relevant data (for instance a Bank class)
– Let the class implement Serializable
– Use the class for writing/reading data
• This is extremely useful for saving inmemory data to a file
RHS – SWC
15
Object streams
public class Bank implements Serializable {...}
// Writing Bank data to a file
Bank myBank = new Bank();
... // Enter data into myBank object
FileOutputStream fos = new FileOutputStream(”bankA.dat”);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(myBank);
// Reading Bank data from a file
FileInputStream fis = new FileInputStream(”bankB.dat”);
ObjectInputStream ois = new ObjectInputStream(fis);
myBank = (Bank)ois.readObject();
RHS – SWC
16