1 / 12

Accessing Files in Java

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);

yakov
Download Presentation

Accessing Files in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Accessing Files in Java Lesson 5

  2. 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

  3. 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

  4. 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

  5. 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; }

  6. 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

  7. 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

  8. 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); }

  9. 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; }

  10. 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

  11. 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) { } } }

  12. Results of the File Read • Line printed by the printRec method

More Related