this week and next tuesday 11am – io friday 10am – io and handout of mini -assignment friday 12...
TRANSCRIPT
![Page 1: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/1.jpg)
This week and next
• Tuesday 11am – IO• Friday 10am – IO and handout of mini -assignment• Friday 12 noon – review – probably of CS12230
exam?
• Monday 1pm – IO• Tuesday 11am – IO (if needed)• Friday – over to Chris
1
![Page 2: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/2.jpg)
First, the world’s quickest review
What are the 3 main characteristics of OO?– Decomposition of problem by data– Encapsulation and Information hiding– Inheritance and Polymorphism
2
Student
-name: String-address: Address
DegreeScheme
-name: String
Module
-name: String
![Page 3: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/3.jpg)
3
Person
-name: String-address: Address
DegreeScheme
-name: String
Module
-name: String
StudentStaff
0..* 0..*
0..*
1..1
This leads to links between classes (and objects)
![Page 4: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/4.jpg)
In next week
• We will focus on reuse through inheritance in order to do Input and Output
4
![Page 5: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/5.jpg)
CS12420IO and Exceptions
(This was also touched on in CS12230 and CS12130?)
5
![Page 6: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/6.jpg)
0. You need to be able to save databetween program executions
• In a Database – next year• In files – not sure how much you did last
semester• We’ll look at a few ways now:
– Human readable (as text files)– Whole objects (Serializable)– XML
![Page 7: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/7.jpg)
CS12420
The java.io.File class• Provides facilities for file manipulation File f = new File("grades.dat");• Can now use
– exists()– canWrite() / canRead()– isFile() / isDirectory()– renameTo(File newName) / delete()– mkdir() / list () (if directory)– lastModified()– length()– createNewFile()
![Page 8: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/8.jpg)
CS12420
Files are based on StreamsStream Fundamentals
filtered flowbuffered flow
data in asource (e .g. a
file )flow of data
InputStream
DataInputS tream
BufferedInputStream
• Streams read from a source and pass data to a sink - stream can be a source and/or a sink
• Streams build on each other – if ‘text’ is a filename:BufferedReader br = new BufferedReader
(new InputStreamReader(new(FileInputStream("text"))));
What do buffered
and filtered mean?
![Page 9: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/9.jpg)
CS12420
No Buffering of Data reading a byte arrayYourClass FileInputStream
read(byte[])
Bytes read from disk
Bytes returned in your byte array (this is the rawest form)
• One problem here is efficiency of disk accesses– Every read can potentially lead to a disk head move
• Buffering improves efficiency…
![Page 10: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/10.jpg)
CS12420
YourClassFileInputStream
Bytes read from disk
BufferedInputStream
read(byte[], …) If buffer empty, read a whole buffer worth of bytes
Bytes returned in yourbyte array
Bytes returned to fillinternal buffer
• This solves the efficiency issue but your classes normally wish to read ints, doubles etc NOT bytes!
• Filtering presents a better interface for your classes to use…
Buffering of Data reading a byte array
If buffer not empty – fill from here
![Page 11: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/11.jpg)
11
Filtering Data reading an int
Bytes read from disk
Returns an int 4 bytes returned for the int
YourClass
FileInputStream
BufferedInputStream
DataInputStream
Read 4 bytes for intreadInt()
Read bytes if emptyBytes returned to fillinternal buffer
![Page 12: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/12.jpg)
CS12420
Categories of stream• Defined in java.io• Read and write streams• “node” streams - connected to a basic source
(or sink) e.g. a file• “filter” and “buffer” streams - connected to
another stream as a source (or sink)• Lots of stream classes - they fit together
![Page 13: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/13.jpg)
CS12420
Stream class hierarchy
Object
InputStream
OutputStream
PrintStream
Reader
InputStreamReader
BufferedReader
FileInputStream
FileReader
StringReader
LineNumberInputStream
DataInputStream
FilterInputStream
ObjectInputStream
SequencenputStream
BufferedInputStream
FilterReader
Writer
DataOutputStream
FilterOutputStream BufferedOutputStream
FileOutputStream
ObjectOutputStream
OutputStreamWriter
StringWriter
FilterWriter
FileWriter
BufferedWriter
![Page 14: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/14.jpg)
CS12420
Reading textBufferedReader br = new BufferedReader(new InputStreamReader( new(FileInputStream("text"))));
Two Alternative short cuts:BufferedReader br = new BufferedReader(new FileReader("text"));
String st = br.readLine(); br.close();Scanner infile =new Scanner(new InputStreamReader (new FileInputStream(“text”))); String st = infile.nextLine(); infile.close();
FileInputStream(bytes)
InputStreamReader
BufferedReader
(strings)
Human readable
![Page 15: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/15.jpg)
Writing text
• Text output use a PrintWriter - can essentially use a PrintWriter to write to a file just like you would use System.out
PrintWriter pw = new PrintWriter(new OutputStreamWriter (new FileOutputStream (“text”)));
pw.println("John");pw.close();
• There is also a BufferedWriter which has different methods and can generate more exceptions
![Page 16: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/16.jpg)
What does this mean for you?
public void load(String fn) throws IOException{ Scanner infile =new Scanner(newInputStreamReader (new FileInputStream(fn)));
int num=infile.nextInt(); infile.nextLine(); //why?
for (int i=0;i<num;i++) { String n=infile.nextLine(); String p=infile.nextLine(); Contact c=new Contact(n,p); contacts.add(c); } infile.close(); }
Aberystwyth University - CS12420
For me:I find an incantation that works and stick with it – this is mine
public void save(String fn) throws IOException{
PrintWriter outfile = new PrintWriter (new OutputStreamWriter (new FileOutputStream (fn))); outfile.println(contacts.size());
for (Contact c:contacts) { outfile.println(c.getName() );
outfile.println(c.getPhone() );//so as to match loading!
} outfile.close(); }
Date file looks like2Fred01970622452Sue01654234234
contacts is an ArrayList
![Page 17: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/17.jpg)
Do I need to review ArrayList?
• Or introduce it?• You are welcome to use any other kind of
collection if you like (CS121 what did you do?)
17
![Page 18: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/18.jpg)
CS12420
Reading data directly (skip)
DataInputStream dis = new DataInputStream(new BufferedInputStream( new FileInputStream("data")));int i = dis.readInt();dis.close();
FileInputStream(bytes)
DataInputS tream
BufferedInputStream
(int, float, short)
![Page 19: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/19.jpg)
CS12420
Writing data directly (skip) • Writing data requires output streamsDataOutputStream dos = new DataOuputStream(new BufferedOutputStream( new FileOutputStream(“data”)));
![Page 20: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/20.jpg)
Person.java example (skip)• See online code example (or next page)• This saves the object’s data using normal IO
stream class operations – faster than as text• The static read method is used since we run
read on the Person class and after it reads the data it constructs a Person object which it returns
![Page 21: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/21.jpg)
public void write(String pathname) throws IOException{ DataOutputStream dos = new DataOutputStream(new
BufferedOutputStream(new FileOutputStream(pathname))); dos.writeUTF(this.name); dos.writeInt(this.age); dos.close(); } public static Person read(String pathname) throws IOException{ DataInputStream dis = new DataInputStream(new
BufferedInputStream(new FileInputStream(pathname))); String theName = dis.readUTF(); int theAge = dis.readInt(); return new Person(theName, theAge); }
![Page 22: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/22.jpg)
1. Exceptions and Text Files
• What Java does when you have a run-time error.• Examples??
• You can either throw on or catch Exceptions• First, look at them in the context of reading from
a text (editor produced) file
22
![Page 23: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/23.jpg)
Trivially – reading a filepublic void load(String fn) throws IOException{ Scanner infile =new Scanner(new InputStreamReader (new FileInputStream(fn))); int num=infile.nextInt();infile.nextLine(); for (int i=0;i<num;i++) { String n=infile.nextLine(); int a=infile.nextInt(); infile.nextLine(); //note this Person c=new Person(n,a); contacts.add(c); } infile.close();}
23
Of the form:2Fred21Mary23
![Page 24: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/24.jpg)
And writing ….
public void save(String fn) throws IOException{ PrintWriter outfile = new PrintWriter(new
OutputStreamWriter(new FileOutputStream(fn))); outfile.println(contacts.size()); for (Person p:contacts) { outfile.println(p.getName()); outfile.println(p.getAge()); //as to match loading! } outfile.close();}
24
In form:2Fred21Mary23
![Page 25: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/25.jpg)
Better
public void load(String fn) {try {… possibly problem code}catch (IOException e) {… what to do}finally{ // (usually omitted)… no matter what
}25
![Page 26: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/26.jpg)
What if you have several possible things that can go wrong?
try{….}catch (IOException e) {…}catch (InputMismatchException e){…}catch (Exception e) { //this catches everything – so put last if at all!…..}
26
Notice how this all relates to inheritance.Exception is the most
general oneIOException is a special
case of it, FileNotFoundException is a special case of that
See API
![Page 27: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/27.jpg)
Dealing with InputMismatch Exceptions
boolean ok=false; do{ try{ System.out.println("enter number"); Scanner in=new Scanner(System.in); int input=in.nextInt(); //may throw exception in.nextLine(); System.out.println("you entered "+input); ok=true; } catch (InputMismatchException e) { //so ‘ok’ is still false } }while(!ok);
27
![Page 28: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/28.jpg)
Beware!• You CANNOT deal with NullPointerExceptions
this way – they are a sign that there is a logic error.
• Usually that you have done something like:Person p;….p.methodcall();
• Without ever saying p=new Person();
28
![Page 29: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/29.jpg)
Day 2
29
![Page 30: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/30.jpg)
You can define your own exceptions
• To deal with ‘exceptional’ situationspublic class MyException extends RuntimeException {
……}
• Then when one of those situations happens say throw new MyException(“message”);
30
![Page 31: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/31.jpg)
do { // The exception is expected to be thrown in the try try { System.out.print("Please enter your age: "); ageOfPerson = in.nextInt(); if (ageOfPerson<0 || ageOfPerson>130) throw new MyInvalidAgeException(); in.nextLine(); successful = true; } // if an exception is thrown, it is caught here catch (InputMismatchException e){ System.out.println("That wasn't an integer " + e); in.nextLine(); } catch (MyInvalidAgeException e) { System.out.println("That was a ridiculous age " + e); in.nextLine(); } finally { System.out.println("I have just executed the finally block"); } } while (!successful); System.out.println("Ok success - you typed in " + ageOfPerson); }
31
![Page 32: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/32.jpg)
2. Serializable
• What is the matter with always using text files?• Have to save each attribute AND consider if more than
one kind of thing all linked (Monsters, Treasures, Weapons) would have to save and link!
• It is possible to save an entire object• Even if it contains links to other objects• Trouble is that the resulting file is only readable by a
computer – not a human like a text file is• Let us see what this means ….
32
![Page 33: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/33.jpg)
Here is part of the class diagram from lecture 1
![Page 34: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/34.jpg)
• Consider the Facebook system (lots of Users, lots of Posts)
• Now consider some point in time where you have 2 users, each has several posts
• Draw the object (instance) diagram for that situation <<do it>>
• Once you have done that, think about how you could store the data in simple text files
![Page 35: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/35.jpg)
Users file:
2FredMary
Posts file:
5
Fred post 1 info
Fred post 2 info
Mary post 1 info
Mary post 2 info
Fred post 3 info
Not very satisfactory is it!
Have to relink all the data in the program
OR
One file:
2
Fred
3
Fred post 1 info
Fred post 2 info
Fred post 3 info
Mary
2
Mary post 1 info
Mary post 2 info
![Page 36: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/36.jpg)
Look at your object diagram
• Wouldn’t it be great if we could just dump the whole system object out when we save and then reload it all at once ..
• .. complete with all the links between objects, no matter how complex (consider if users are tagged in other users posts)
• We can ….
![Page 37: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/37.jpg)
Serializable interface
public class Person implements Serializable{…..}• Serializable is a marker interface: it has no methods,
but is used to mark the class to give the serialization mechanism permission to serialize objects of the class
• Then there are some clever methods that enable a stream to read and write these whole objects
• We then use these
37
![Page 38: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/38.jpg)
This uses a different kind of File Byte Stream as opposed to a Character Stream MACHINE
readable not HUMAN readable
public void writeToFile(String fullPathOfFile) throws IOException { // Lets get a handle on the file to write to File myFile = new File (fullPathOfFile); ObjectOutputStream myStream = new ObjectOutputStream( new BufferedOutputStream( new FileOutputStream(myFile))); System.out.println("Writing to the file now...."); myStream.writeObject(this); // Write the object to file myStream.close(); // Don't forget this!! }
38
![Page 39: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/39.jpg)
How USE this method
Person p; //presumably gets filled somewhere……p.writeToFile(“out.ser”);
39
![Page 40: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/40.jpg)
Typical Serialised file shown in editor:
This contains the Person record with name “fred” age 41¬ísrPerson—ËÁHrT“IageLnametLjava/lang/String;xp)tfred
40
![Page 41: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/41.jpg)
What is actually saved is
1. Class name of the object2. Object’s primitive data3. If attributes are object references, then those
objects are also saved (go to point 1)4. Each object has a unique serial version number
to ensure it’s not saved twice and that the versions read and written match up properly
5. Not saved: Methods or static variables/constants
41
![Page 42: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/42.jpg)
But reading has to be done differently
p.readPerson(“out.ser”) would not work because there IS no p until we finish reading.
Instead we need a method that returns a Person:p=Person.readPerson(“out.ser”);
This is an example of a static method (linked directly to the class rather than called for a particular object of the class)
42
![Page 43: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/43.jpg)
Here is the code://notice the staticness public static Person readPerson(String fullPathOfFile) throws IOException{ Person tempPerson; File myFile = new File (fullPathOfFile); ObjectInputStream myStream = new ObjectInputStream( new BufferedInputStream(new FileInputStream(myFile))); System.out.println("Reading from the file now...."); // Read the object from file - it needs to be cast try { tempPerson = (Person) myStream.readObject(); } catch(ClassNotFoundException e) { throw new IOException("File had objects of incorrect class"); } myStream.close(); return tempPerson; } 43
![Page 44: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/44.jpg)
Aside: Other static method examples
Collections.sort(someList);
double x=Math.sqrt(9.0);
double x=Math.pow(3.0,2.0);
44
![Page 45: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/45.jpg)
What can go wrong?
• NotSerializableException at runtimeThat means that you haven’t made the thing to
serialize AND all the things it points to implement Serializable
• InvalidClassException at runtimeThat happens if you serialize an object and then
change the definition of the object’s class and then try to deserialize it the unique serial version numbers do not match …
45
![Page 46: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/46.jpg)
• If you change attributes then we want Java to throw this exception, otherwise we will lose data etc (bugs)
• However, if we just make a small change (such as adding a method) then we don’t care and we don’t want to see the exception
• In your programs you are unlikely to have a problem since you won’t be using files generated many versions ago
![Page 47: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/47.jpg)
If it becomes a problem you will need to investigate the ‘serialver’ command (serial version command)
– You can generate a serialver yourself in your codeprivate static final long serialVersionUID = 1L;
– On the command lineserialver Person2
Do this before you make the change and before serializing the objectNow when you make a minor change and deserialize Java will use the serial version number defined in that static final and no exception will be thrown
– Or through eclipse
![Page 48: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/48.jpg)
Beware trying to serialize Scanner
• If you keep your data separate from your manipulation (so the thing you are serializing does not have a private Scanner instance variable) you won’t have a Scanner in the stuff you are trying to save
But if not …• You should make a Scanner ‘transient’ so that
the system doesn’t try to Serialize itprivate transient Scanner scan;
48
![Page 49: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/49.jpg)
3. Another way of I/O whole objects
• This is another nice way to I/O entire objects• XML – have you run across this before?• Extensible Markup Language (XML) is a
markup language created to structure, store, and transport data by defining a set of rules for encoding documents in a format that is both human-readable and machine-readable. (wikipedia)
49
![Page 50: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/50.jpg)
This is the result:
50
![Page 51: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/51.jpg)
This is how you get it:import java.io.*;import java.beans.XMLEncoder;import java.beans.XMLDecoder;///////////////////////////////////////////////// must have ALL gets and sets – must be a ‘bean’public class PersonXML{ private int age; private String name;//lots deleted
public void write(String pathname) throws IOException{ XMLEncoder encoder = new XMLEncoder(
new BufferedOutputStream(new FileOutputStream(pathname))); encoder.writeObject(this); encoder.close(); }
public static PersonXML read(String pathname) throws IOException { PersonXML result = null; XMLDecoder decoder = new XMLDecoder( new BufferedInputStream( new FileInputStream(pathname))); result = (PersonXML)decoder.readObject(); return result; }}
51
Class must be a bean -have gets and sets for all attributes and a default constructor!
![Page 52: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/52.jpg)
• See notes 1-exceptions.doc• And code 1-exceptions-serializable-files
52
![Page 53: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/53.jpg)
What have we just done?• Exceptions• A couple of things about files and IO
• Reading/Writing files• Serialization• XMLEncoder and Decoder
• Do you notice that a lotof this depends on inheritance??
• Look at inheritance hierarchy for an Exception• Look at Serializable and XMLEncoderhttp://docs.oracle.com/javase/7/docs/api/
53
Save these slides for reference in your Java programming careerI always have to look this stuff up, but if you understand the principles that is not a problem!
![Page 54: This week and next Tuesday 11am – IO Friday 10am – IO and handout of mini -assignment Friday 12 noon – review – probably of CS12230 exam? Monday 1pm –](https://reader036.vdocuments.net/reader036/viewer/2022070408/56649e565503460f94b4e704/html5/thumbnails/54.jpg)
Mini Assignment 1 – 10%• Details on Blackboard• You will get 3 mini assignments in which you will be
asked to try out things (also smaller worksheets)• We started this so that you wouldn’t have too many
big assignments but would still get to practise everything we do in this module as it happens – ie. you’d just have one ‘real’ assignment
• Then last year some people didn’t do the first one – 10% is a lot to lose!
54