1 3 5 22 23 package edu.neu.ccs.jmk; 24 25 import java.io.*; 26 27 34 abstract class BinaryFileOperator 35 implements Operator 36 { 37 38 44 abstract boolean exec(File arg1, File arg2, PrintWriter out); 45 46 54 public void exec(String [] args, PrintWriter out) 55 throws CommandFailedException 56 { 57 args = FileOperator.glob(args); 58 if (args.length != 2) { 59 String msg = getName() + " expects 2 operands, but got " + args.length; 60 throw new CommandFailedException(msg); 61 } 62 if (!exec(new File(args[0]), new File(args[1]), out)) { 63 String msg = getName() + " " + args[0] + " " + args[1] + 64 " operation failed"; 65 throw new CommandFailedException(msg); 66 } 67 } 68 69 74 public static void main(String [] args) { 75 PrintWriter out = new PrintWriter(System.out, true); 76 BinaryFileOperator operator = new BinaryFileOperator() { 77 boolean exec(File arg1, File arg2, PrintWriter out) { 78 return arg1.renameTo(arg2); 79 } 80 public String getName() { 81 return "rename"; 82 } 83 }; 84 85 try { 86 operator.exec(args, out); 87 } 88 catch (Throwable t) { 89 System.err.println("Internal error: " + t.getMessage()); 90 t.printStackTrace(); 91 } 92 out.println("Binary file command " + operator.getName() + " completed"); 93 } 94 } 95 | Popular Tags |