home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 2.8 KB | 100 lines |
- import java.io.*;
-
- /*
- Format.class translates a text file
- * to either DOS, Mac, or UNIX
- * format. The differences are in line termination.
- */
- class Format {
-
- static final int TO_DOS = 1;
- static final int TO_MAC = 2;
- static final int TO_UNIX = 3;
- static final int TO_UNKNOWN = 0;
-
- static void usage () {
- System.out.print ("usage: java Format -dmu <in-file> ");
- System.out.println ("<out-file>");
- System.out.println ("\t-d converts <in-file> to DOS");
- System.out.println ("\t-m converts <in-file> to MAC");
- System.out.println ("\t-u converts <in-file> to UNIX");
- }
-
- public static void main (String args[])
- {
- int format=TO_UNKNOWN;
- String buf;
- FileInputStream fsIn = null;
- FileOutputStream fsOut = null;
-
- if (args.length != 3) { // you must specify format, in, out
- usage ();
- System.exit (1);
- }
-
- /*
- args[0] is a String, so we can use
- * the equals (String) method for
- * comparisons
- */
- if (args[0].equals ("-d")) format = TO_DOS; else
- if (args[0].equals ("-m")) format = TO_MAC; else
- if (args[0].equals ("-u")) format = TO_UNIX; else {
- usage ();
- System.exit (1);
- }
-
- try {
- fsIn = new FileInputStream (args[1]);
- } catch (Exception e) {
- System.out.println (e);
- System.exit (1);
- }
-
- /*
- * FileOutputStream is the complement of FileInputStream
- */
- try {
- fsOut = new FileOutputStream (args[2]);
- } catch (Exception e) {
- System.out.println (e);
- System.exit (1);
- }
- BufferedReader dsIn = new BufferedReader(new InputStreamReader(fsIn));
-
- PrintStream psOut = new PrintStream (fsOut);
- while (true) {
- try {
- buf = dsIn.readLine ();
- if (buf == null) break; // break on EOF
- } catch (IOException e) {
- System.out.println (e);
- break;
- }
- psOut.print (buf);
- switch (format) {
- case TO_DOS:
- psOut.print ("\r\n");
- break;
-
- case TO_MAC:
- psOut.print ("\r");
- break;
-
- case TO_UNIX:
- psOut.print ("\n");
- break;
- }
- }
-
- /*
- */
- try {
- fsIn.close ();
- fsOut.close ();
- } catch (IOException e) {
- System.out.println (e);
- }
- }
- }
-