120 likes | 272 Vues
Accessing Files in Java. Lesson 5. Reading Text from a File. Use FileInputStream and DataInputStream File types are “wrapped” FileInputStream in = new FileInputStream ("nameNumber.txt"); DataInputStream s = new DataInputStream (in);
E N D
Accessing Files in Java Lesson 5
Reading Text from a File • Use FileInputStream and DataInputStream • File types are “wrapped” FileInputStream in = new FileInputStream ("nameNumber.txt"); DataInputStream s = new DataInputStream (in); • Command then used is stringVal = s.readLine() • Note Source code of readStrings.java
Program Features • Note the specification of exception handlingpublic static void main(String args[ ]) throws IOException • This requires a try block and a catch blockcatch (IOException ioe) { } • Note contents of input file and resulting output
Declaring a Class • Declared in a separate file – nameRec.java • Methods declared, implemented within the class declaration • Note use of public specifiers (some things can also be private) • For objects of this class to be saved as binary file records, implements Serializablemust be declared • Class will be used by another program
Must specify this for saving objects of this type to a file Data attributes Constructor Declaring a Class public class nameRec extends Object implements Serializable{ public String name; public double dNum; public int iNum; public void nameRec() { name = ""; dNum=0.0; iNum = 0; } public String getName() { return name; }
Declaring a Class public void setRec (String n, double d, int i) { name = n; dNum = d; iNum = i; } public void nameRec(String n, double d, int i) { setRec (n, d, i); } public void printRec () { System.out.println ("Record : "+name+" "+dNum+" "+iNum); } Another constructor
Creating a Binary Data File • ReadNameNum.java • nameRec object instantiated • Open the text file to be readFileInputStream wrapped in DataInputStream • Open the file for nameRec object outputFileOutputStream wrapped in ObjectOutputStream
Creating a Binary Data File • Prime the pump • Check forend of file • Process therecord • Cast objectas it is written • Try to read the next record nRec = readRecord (in); while (nRec.getName() != null) { nRec.printRec(); s.writeObject ((nameRec) nRec); nRec = readRecord(in); }
Reading the data Change strings to Integer and Double “wrapped” objects Call nameRec set method Manipulating Text to Load the Object public static nameRec readRecord (DataInputStream in) throws IOException { String name = null, iString = null, dString = null; name = in.readLine(); iString = in.readLine(); dString = in.readLine() ; nameRec temp = new nameRec (); if (dString == null) return temp; // no more records Double d = Double.valueOf(dString); Integer i = Integer.valueOf(iString); int ii = i.intValue(); double dd = d.doubleValue(); temp.setRec (name, dd,ii); return temp; }
Reading Objects from a File • readNameRec.java • Note • import nameRec; • The wrapping of the FileInputStream in the ObjectInputStream file • Instantiation of the nameRec object • throw, try, and catch specifications
Error handling specifications Do the I/O in the condition Reading Objects from a File public class readNameRec { public static void main (String[] args) throws IOException, ClassNotFoundException { ObjectInputStream s = (new ObjectInputStream (new FileInputStream ("nameNum.dat"))); nameRec nRec = new nameRec(); try { while ((nRec = (nameRec)s.readObject()) != null) { nRec.printRec(); } } catch (IOException ioe) { } catch (ClassNotFoundException cnfe) { } } }
Results of the File Read • Line printed by the printRec method