Download presentation
Presentation is loading. Please wait.
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 handling public static void main(String args[ ]) throws IOException This requires a try block and a catch block catch (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 Serializable must be declared Class will be used by another program
5
Must specify this for saving objects of this type to a file
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; } Must specify this for saving objects of this type to a file Data attributes Constructor
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 read FileInputStream wrapped in DataInputStream Open the file for nameRec object output FileOutputStream wrapped in ObjectOutputStream
8
Creating a Binary Data File
Prime the pump Check for end of file Process the record Cast object as 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
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 the data Change strings to Integer and Double “wrapped” objects Call nameRec set method
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
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) { } Error handling specifications Do the I/O in the condition
12
Results of the File Read
Line printed by the printRec method
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.