/* * NOTE for hw3: * * Since the interface "nodeInterface" in hw2 * is different from the interface "nodeInt" in hw3, * the "readFromFile" program from hw2 must * be slightly modified. If you figured it * out, fine. Otherwise, here is the revised * version (meant for Unix/Linux). You can * do the obvious changes to get this to work * for Windows. */ //================================================== // readFromFile( fn ) : constructs a list from a file "fn" //================================================== public int readFromFile(String fn) // returns the number of nodes in list // the first node in the read-in list is "this" // if the returned value is <0, error in opening files { FileInputStream is = null; DataInputStream dis = null; try { is = new FileInputStream(fn); dis = new DataInputStream(is); } catch (Exception e) { System.out.println("Fail to Open"); return -1; } int count = 0; nodeInt curr = new myNode(); nodeInt last = this; if (curr.readString(dis) < 0) { return -1; } else { ++count; last.setItem(curr.getItem()); curr = new nodeInt(); while (curr.readString(dis) >= 0) // allow 0 length item { last.setNext(curr); last = last.getNext(); curr = new nodeInt(); ++count; } } return count; } //readFromFile