Download presentation
Presentation is loading. Please wait.
Published byVerity Clark Modified over 8 years ago
1
Streams
2
RHS – SWC 2 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
3
RHS – SWC 3 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
4
RHS – SWC 4 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
5
RHS – SWC 5 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();
6
RHS – SWC 6 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();
7
RHS – SWC 7 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
8
RHS – SWC 8 Binary files – random access A file also has a file pointer The file pointer indicates where to read/write the next time 197705129
9
RHS – SWC 9 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();
10
RHS – SWC 10 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
11
RHS – SWC 11 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…
12
RHS – SWC 12 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
13
RHS – SWC 13 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
14
RHS – SWC 14 Object streams public interface Serializable { // Empty... }
15
RHS – SWC 15 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 in- memory data to a file
16
RHS – SWC 16 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();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.