import java.io.*;
import java.util.*;
import javax.swing.*;

/** Class to demo input and output */
public class IOStreams {
    
    /** 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 InputStream getInputStream(String p) {
        try {
            JFileChooser jd;
            if (p == null) {
                jd= new JFileChooser();
            }
            else {
                jd= new JFileChooser(p);
            }
            jd.setDialogTitle("Choose input file");
            jd.showOpenDialog(null);
            
            return new FileInputStream(jd.getSelectedFile());
        } 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 OutputStream getOutputStream(String p) {
        try {
            JFileChooser jd;
            if (p == null) {
                jd= new JFileChooser();
            }
            else {
                jd= new JFileChooser(p);
            }
            jd.setDialogTitle("Choose output file");
            jd.showSaveDialog(null);
            File f= jd.getSelectedFile();
            if (f == null) {
                return null;
            }
            return new FileOutputStream(f);
        } catch (IOException e) {
            return null;
        }
    }  
}