<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import java.io.*;
import java.util.*;
import javax.swing.*;

/** Class to demo reading and writing files
  */
public class LabIO {
    
    /** = the number of lines in a file chosen by the caller. Close
          the file when done with it. If the reader cancels the
          dialog (for obtaining the file), return 0. */
    public static int lines() throws IOException {
        BufferedReader bf= getReader(null);
        if (bf == null) {
            return 0;
        }
        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();
        }
        
        bf.close();
        return n;
    }
    
    /** Prompt the user for an input file; then read the file and
        print each line that contains an asterisk '*';
        finally, close the file.
        If the reader cancels the dialog, don't do anything.*/
    public static void checkFile() {
       
        
    }
    
    /** Obtain an output file name from the user and print on it
        all the strings in array b, each on its own line.
        If the user cancels the dialog to find the file name,
        print a message and return. */
    public static void writeFile(String[] b) {
 
    }
    
    
    /** Obtain a file name from the user using a JFileChooser)
      and return a reader that is linked to it. If the user cancels the
      dialog window and thus does not give a file name, return null.
      Parameter p can be a path on the hard drive or null. If p is not null,
      start the JFileChooser at the path given by p.*/
    public static BufferedReader getReader(String p) throws IOException {
        try {
            JFileChooser jd;
            if (p == null) {
                jd= new JFileChooser();
            }
            else {
                jd= new JFileChooser(p);
            }
            
            jd.setDialogTitle("Choose input file");
            jd.showOpenDialog(null);
            
             if (jd == null)
                return 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));
    }
}
</pre></body></html>