CS1083 Chapter 11: File Error Handling, Object Serialization.

squawkpsychoticΛογισμικό & κατασκευή λογ/κού

2 Δεκ 2013 (πριν από 3 χρόνια και 4 μήνες)

71 εμφανίσεις

CS1083
Chapter 11:File Error Handling,Object
Serialization.
David Bremner
2011-02-01
Error Handling and prevention I:who throws what
FileInputStream(String)
I
FileNotFoundException (checked)
I
SecurityException (unchecked)
FileOutputStream(String)
I
FileNotFoundException (checked)
I
What the heck?Check documentation...
I
SecurityException
read(byte)/write(byte)
IOException (checked,but unhandlable).
A robust le copy I
BufferedInputStream in=null;
if (args.length <2) usage();
tryf
in= new BufferedInputStream(
new FileInputStream(args[0]));
g catch (FileNotFoundException e)f
System.out.println("bad input:"+args[0]);
System.exit(1);
g
BufferedOutputStream out=null;
tryf
out =new BufferedOutputStream(
new FileOutputStream(args[1]));
g catch (FileNotFoundException e)f
System.out.println("bad output:"+args[1]);
System.exit(1);
g
Robust le copy II
long bytes=0;
try f
int b=in.read();
while (b>=0)f
bytes++;
out.write(b);
b=in.read();
g
g catch (IOException e)f
System.out.println(e.getMessage());
System.exit(1);
g
Class File:An ounce of prevention
MetaData
I
File permissions
I
Directory listing
I
Size
I
File vs.Directory
Check Before Opening
File inFile=new File(args[0]);
if (!inFile.canRead())f
System.out.println("Could not open input"
+args[0]);
System.exit(1);
g
File outFile=new File(args[1]);
if (!outFile.canWrite())f
System.out.println("Could not open output"
+args[1]);
System.exit(1);
g
Simplied exception handling
try f
BufferedInputStream in=
new BufferedInputStream(
new FileInputStream(args[0]));
BufferedOutputStream out =
new BufferedOutputStream(
new FileOutputStream(args[1]));
int b=in.read();
while (b>=0)f
bytes++;
out.write(b);
b=in.read();
g
g catch (IOException e)f
System.out.println(e.getMessage());
System.exit(1);
g
unix% java FileCopy5 mystery.dat/usr/mystery.dat
Could not open output le/usr/mystery.dat
unix% java FileCopy5 mystery.dat/usr
Could not open output le/usr
unix% java FileCopy5 foo bar
Could not open input le foo
unix%
Object Streams
I
ObjectInputStream and ObjectOutputStream transform
objects to and from sequences of bytes
I
This is called Serialization
I
In order to work properly,a class,all of its object variables,
and of it superclass must implement interface Serializable
publ i c c l a s s Bitmap i mpl ements Serializablef
pr i v a t e i nt [ ] [ ] bits;
pr i v a t e i nt columns;
pr i v a t e i nt rows;
.
.
.
g
Writing it out
publ i c s t a t i c voi d main( String [ ] args) thr ows IOExceptionf
Bitmap bitmap=new Bitmap("noi s y.pbm");
ObjectOutputStream out=new ObjectOutputStream(
new FileOutputStream("noi s y.bi n") );
out.writeObject( bitmap );
out.close ( );
g
Yes?od -c noisy.bin | head -8
0000000 254 355\0 005 s r\0 006 B i t m a p 305,
0000020 e 341 5 207\'N 002\0 003 I\0\a c o l u
0000040 m n s I\0 004 r o w s [\0 004 b i t
0000060 s t\0 003 [ [ I x p\0\0\0 4\0\0\0
0000100.u r\0 003 [ [ I 027 367 344 O 031 217 211 <
0000120 002\0\0 x p\0\0\0.u r\0 002 [ I M
Yes?
Reading it back
publ i c s t a t i c voi d main( String [ ] args) thr ows
IOException,ClassNotFoundExceptionf
ObjectInputStream in=new ObjectInputStream(
new FileInputStream("noi s y.bi n") );
Bitmap bitmap=(Bitmap) in.readObject ( );
in.close ( );
System.out.println( bitmap );
g
I
Why the ClassNotFoundException
import java.io.Serializable;
public class Person implements Serializable f
private String myName;
private int myPets;
public String toString()f
return"Person [ name ="+ myName +
";pets="+myPets+"]";
g
public Person(String name,int pets)f
myName=name;
myPets=pets;
g
g
import java.util.Vector;
import java.io.Serializable;
public class PeopleVec extends Vector<Person>
implements Serializable f
public void add(String name,int pets)f
add(new Person(name,pets));
g
public Person getPerson(int index)f
return get(index);
g
g
public static void main(String [] args)
throws java.io.IOException f
ObjectOutputStream out =
new ObjectOutputStream(
new FileOutputStream("people.dat"));
PeopleVec PV=new PeopleVec();
String [] names=f"Bob","Doug","Mohona",
"Takeshi","Wenjie"g;
for (int i=0;i<names.length;i++)f
PV.add(names[i],i);
g
out.writeObject(PV);
out.flush();
g
public static void main(String [] args)
throws java.io.IOException,
java.lang.ClassNotFoundExceptionf
ObjectInputStream in =
new ObjectInputStream(
new FileInputStream("people.dat"));
PeopleVec PV= (PeopleVec)in.readObject();
for (int i=0;i<PV.size();i++)f
Person somebody=PV.getPerson(i);
System.out.println(somebody);
g
g
One More time,without Vectors
public static void main(String [] args)
throws java.io.IOException f
ObjectOutputStream out =
new ObjectOutputStream(
new FileOutputStream("people2.dat"));
String [] names=f"Bob","Doug","Mohona",
"Takeshi","Wenjie"g;
for (int i=0;i<names.length;i++)f
out.writeObject(new Person(names[i],i));
g
out.flush();
g
Reading a Stream of objects
public static void main(String [] args)
throws java.io.IOException,
java.lang.ClassNotFoundExceptionf
ObjectInputStream in =
new ObjectInputStream(
new FileInputStream("people2.dat"));
try f
while(true)f
Person somebody=(Person)in.readObject();
System.out.println(somebody);
g
g
catch (java.io.EOFException e)fg
g