Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import javax.swing.*;
  4.  
  5. /** Class to demo reading and writing files
  6.   */
  7. public class LabIO {
  8.    
  9.     /** = the number of lines in a file chosen by the caller. Close
  10.           the file when done with it. If the reader cancels the
  11.           dialog (for obtaining the file), return 0. */
  12.     public static int lines() throws IOException {
  13.         BufferedReader bf= getReader(null);
  14.         if (bf == null) {
  15.             return 0;
  16.         }
  17.         String lin= bf.readLine();
  18.         int n= 0;
  19.         // invariant: n is the number of lines that precede line lin, and
  20.         //            lin is the next line to process (null if no more).
  21.         while (lin != null) {
  22.             // Process line lin
  23.             n= n + 1;
  24.            
  25.            
  26.             lin= bf.readLine();
  27.         }
  28.        
  29.         bf.close();
  30.         return n;
  31.     }
  32.    
  33.     /** Prompt the user for an input file; then read the file and
  34.         print each line that contains an asterisk '*';
  35.         finally, close the file.
  36.         If the reader cancels the dialog, don't do anything.*/
  37.     public static void checkFile() throws IOException {
  38.        BufferedReader bf= getReader(null);
  39.          if (bf == null) {
  40.             return;
  41.         }
  42.          String check= bf.readLine();
  43.              while (check != null) {
  44.                  if (check.contains("*")) {
  45.                      System.out.println(check);
  46.  
  47.              }
  48.                  check= bf.readLine();
  49.          }
  50.          bf.close();
  51.     }
  52.    
  53.     /** Obtain an output file name from the user and print on it
  54.         all the strings in array b, each on its own line.
  55.         If the user cancels the dialog to find the file name,
  56.         print a message and return. */
  57.     public static void writeFile(String[] b) {
  58.  
  59.     }
  60.    
  61.    
  62.     /** Obtain a file name from the user using a JFileChooser)
  63.       and return a reader that is linked to it. If the user cancels the
  64.       dialog window and thus does not give a file name, return null.
  65.       Parameter p can be a path on the hard drive or null. If p is not null,
  66.       start the JFileChooser at the path given by p.*/
  67.     public static BufferedReader getReader(String p) throws IOException {
  68.         try {
  69.             JFileChooser jd;
  70.             if (p == null) {
  71.                 jd= new JFileChooser();
  72.             }
  73.             else {
  74.                 jd= new JFileChooser(p);
  75.             }
  76.            
  77.             jd.setDialogTitle("Choose input file");
  78.             jd.showOpenDialog(null);
  79.            
  80.              if (jd == null)
  81.                 return null;
  82.            
  83.             FileReader fr= new FileReader(jd.getSelectedFile());
  84.             return new BufferedReader(fr);
  85.         }
  86.         catch (IOException e) {
  87.             return null;
  88.         }
  89.     }
  90.    
  91.     /** Obtain a file name from the user, using a JFileChooser, and return
  92.       a PrintStream that is linked to it. Return null if the user cancels. */
  93.     public static PrintStream getWriter() throws IOException {
  94.         JFileChooser jd= new JFileChooser();
  95.         jd.setDialogTitle("Choose output file");
  96.         jd.showSaveDialog(null);
  97.         File f= jd.getSelectedFile();
  98.         if (f == null) {
  99.             return null;
  100.         }
  101.         return new PrintStream(new FileOutputStream(f));
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement