1 3 5 22 23 package edu.neu.ccs.jmk; 24 25 import java.io.*; 26 27 32 public final class CreateOperator 33 implements Operator 34 { 35 39 public String getName() { 40 return "create"; 41 } 42 43 50 public void exec(String [] args, java.io.PrintWriter out) 51 throws CommandFailedException 52 { 53 if (args.length == 0) { 54 String 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 msg = getName() + " " + args[0] + " operation failed"; 77 throw new CommandFailedException(msg); 78 } 79 80 85 public static void main(String [] args) { 86 PrintWriter out = new PrintWriter(System.out, true); 87 try { 88 new CreateOperator().exec(args, out); 89 } 90 catch (Throwable t) { 91 System.err.println("Internal error: " + t.getMessage()); 92 t.printStackTrace(); 93 } 94 out.println("Create command completed"); 95 } 96 } 97 | Popular Tags |