home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / javafile / ch02 / Format.java < prev    next >
Encoding:
Java Source  |  1998-12-14  |  2.8 KB  |  100 lines

  1. import java.io.*;
  2.  
  3. /*
  4. Format.class translates a text file 
  5. * to either DOS, Mac, or UNIX
  6. * format.  The differences are in line termination.
  7.  */
  8. class Format {
  9.  
  10. static final int TO_DOS = 1;
  11. static final int TO_MAC = 2;
  12. static final int TO_UNIX = 3;
  13. static final int TO_UNKNOWN = 0;
  14.  
  15. static void usage () {
  16.         System.out.print ("usage: java Format -dmu <in-file> ");
  17.         System.out.println ("<out-file>");
  18.         System.out.println ("\t-d converts <in-file> to DOS");
  19.         System.out.println ("\t-m converts <in-file> to MAC");
  20.         System.out.println ("\t-u converts <in-file> to UNIX");
  21. }
  22.  
  23. public static void main (String args[])
  24. {
  25.         int format=TO_UNKNOWN;
  26.         String buf;
  27.         FileInputStream fsIn = null;
  28.         FileOutputStream fsOut = null;
  29.  
  30.        if (args.length != 3) { // you must specify format, in, out
  31. usage ();
  32.                System.exit (1);
  33.         }
  34.  
  35. /*
  36. args[0] is a String, so we can use 
  37.  * the equals (String) method for
  38. * comparisons
  39.  */
  40.         if (args[0].equals ("-d")) format = TO_DOS; else
  41.         if (args[0].equals ("-m")) format = TO_MAC; else
  42.         if (args[0].equals ("-u")) format = TO_UNIX; else {
  43.                usage ();
  44.                System.exit (1);
  45.         }
  46.         
  47.         try {
  48.               fsIn = new FileInputStream (args[1]);
  49.         } catch (Exception e) {
  50.                System.out.println (e);
  51.                System.exit (1);
  52.         }
  53.  
  54. /*
  55.  * FileOutputStream is the complement of FileInputStream
  56.  */
  57.         try {
  58.                fsOut = new FileOutputStream (args[2]);
  59.         } catch (Exception e) {
  60.                System.out.println (e);
  61.                System.exit (1); 
  62.         }
  63.       BufferedReader dsIn = new BufferedReader(new InputStreamReader(fsIn));
  64.  
  65.         PrintStream psOut = new PrintStream (fsOut);
  66.         while (true) {
  67.               try {
  68.                       buf = dsIn.readLine ();
  69.                       if (buf == null) break;        // break on EOF
  70.               } catch (IOException e) {
  71.                       System.out.println (e);
  72.                       break;
  73.                }
  74.                psOut.print (buf);
  75.                switch (format) {
  76.                       case TO_DOS:
  77.                       psOut.print ("\r\n");
  78.                       break;
  79.                       
  80.                       case TO_MAC:
  81.                       psOut.print ("\r");
  82.                       break;
  83.                       
  84.                       case TO_UNIX:
  85.                       psOut.print ("\n");
  86.                       break;               
  87.               }
  88.         }
  89.         
  90. /*
  91. */
  92.        try {
  93.                fsIn.close ();
  94.                fsOut.close ();
  95.         } catch (IOException e) {
  96.                System.out.println (e);
  97.         }
  98. }
  99. }
  100.