KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > CreateOperator


1 // $Id: CreateOperator.java,v 1.2 2001/12/07 11:41:24 ramsdell Exp $
2

3 // The create operator for creating a file and its contents.
4

5 /*
6  * Copyright 1998 by John D. Ramsdell
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 package edu.neu.ccs.jmk;
24
25 import java.io.*;
26
27 /**
28  * The create operator for creating a file and its contents.
29  * @version April 1998
30  * @author John D. Ramsdell
31  */

32 public final class CreateOperator
33 implements Operator
34 {
35   /**
36    * Get the name of an operator.
37    * @return the name for printing
38    */

39   public String JavaDoc getName() {
40     return "create";
41   }
42
43   /**
44    * The create operator creates a file given by its first operand
45    * and than writes any remaining operands into the file as
46    * separate lines of text.
47    * @param args parameters to the operation
48    * @param out place to write messages
49    */

50   public void exec(String JavaDoc[] args, java.io.PrintWriter JavaDoc out)
51        throws CommandFailedException
52   {
53     if (args.length == 0) {
54       String JavaDoc msg = getName() + " received no operands";
55       throw new CommandFailedException(msg);
56     }
57     try {
58       FileWriter fw = new FileWriter(args[0]);
59       if (args.length == 1)
60     fw.close();
61       else {
62     BufferedWriter bw = new BufferedWriter(fw);
63     PrintWriter pw = new PrintWriter(bw);
64     for (int i = 1; i < args.length; i++)
65       pw.println(args[i]);
66     pw.close();
67       }
68       return;
69     }
70     catch (FileNotFoundException ex) {
71       out.println("Cannot open " + ex.getMessage());
72     }
73     catch (IOException ex) {
74       out.println(ex.toString());
75     }
76     String JavaDoc msg = getName() + " " + args[0] + " operation failed";
77     throw new CommandFailedException(msg);
78   }
79
80   /**
81    * An entry point for testing create commands.
82    * It simply passes the arguments to the create operation.
83    * @param args operands for the command
84    */

85   public static void main(String JavaDoc[] args) {
86     PrintWriter out = new PrintWriter(System.out, true);
87     try {
88       new CreateOperator().exec(args, out);
89     }
90     catch (Throwable JavaDoc t) {
91       System.err.println("Internal error: " + t.getMessage());
92       t.printStackTrace();
93     }
94     out.println("Create command completed");
95   }
96 }
97
Popular Tags