import java.io.*; import java.util.*; import javax.swing.*; /** Class to demo reading of files */ public class Reading { /** Yields: the number of lines in a file chosen by the caller */ public static int lines() throws IOException { BufferedReader bf= getReader(null); String lin= bf.readLine(); int n= 0; // invariant: n is the number of lines that precede line lin, and // lin is the next line to process (null if no more). while (lin != null) { // Process line lin n= n + 1; lin= bf.readLine(); } return n; } /** Yields: "a line of a file chosen by the user contains s" */ public static boolean contains(String s) { return false; // STUB RETURN. Replace. } /** Obtain a file name from the user using a JFileChooser * and return a reader that is linked to it. If p is not null * (it can be), start the JFileChooser at the path given by p*/ public static BufferedReader getReader(String p) { try { JFileChooser jd; if (p == null) { jd= new JFileChooser(); } else { jd= new JFileChooser(p); } jd.setDialogTitle("Choose input file"); jd.showOpenDialog(null); FileReader fr= new FileReader(jd.getSelectedFile()); return new BufferedReader(fr); } catch (IOException e) { return null; } } /** Obtain a file name from the user, using a JFileChooser, and return * a PrintStream that is linked to it. Return null if the user cancels. */ public static PrintStream getWriter() throws IOException { JFileChooser jd= new JFileChooser(); jd.setDialogTitle("Choose output file"); jd.showSaveDialog(null); File f= jd.getSelectedFile(); if (f == null) { return null; } return new PrintStream(new FileOutputStream(f)); } /** Read in a file of Peoplesoft netids for CS1110 and a file of CMS * netids for CS1110 (both using a JFileChooser to retrieve the files). * * Obtain from the user an output file name to write (using a JFileChooser) * * Write on the output file all netids that are in both input files. * * Precondition: both files are in sorted order. */ public static void netIds() throws IOException { /*Note: We give a series of comment-statements stating what the body is to do. Implement each comment-statement UNDERNEATH that comment-statement. and put a blank line after the implementation. This way, you can read the program on two levels --the high-level comment-statements or their implementations. */ // Obtain a BufferedReader peop for the Peoplesoft file. // Obtain a BufferedReader cms for the cms file. // Obtain a PrintStream out for the output file. // Loop through the two input files in a kind of synchronized // fashion (remember, they are sorted), and put the lines // that are in both of them on file out, using out.println(...); // Close file out } }