1 package de.webman.anttasks; 2 3 import java.io.*; 4 import org.apache.tools.ant.BuildException; 5 import org.apache.tools.ant.Task; 6 7 8 30 public class AppendFile extends Task { 31 32 private File infile; 33 private File outfile; 34 boolean failonerror = true; 35 36 public void execute() throws BuildException { 38 String msg = null; 39 if ( infile == null ){ 41 msg = "argument file must be given"; 42 } 43 else if ( outfile == null ){ 44 msg = "argument appendto must be given"; 45 } 46 else if ( ! infile.exists() ){ 47 msg = "file " + infile + " does not exist"; 48 } 49 else if ( ! infile.isFile() ){ 50 msg = "file " + infile + " is not a plain file"; 51 } 52 else if ( ! infile.canRead() ){ 53 msg = "file " + infile + " is not readable"; 54 } 55 else if ( infile.equals(outfile) ){ 56 msg = "cannot append a file to itself"; 57 } 58 else if ( outfile.exists() ){ 59 if ( ! outfile.isFile() ){ 60 msg = "file " + outfile + " is not a plain file"; 61 } 62 else if ( ! outfile.canWrite() ){ 63 msg = "file " + outfile + " is not writeable"; 64 } 65 } 66 if ( msg != null ){ 68 if ( failonerror ){ 69 throw new BuildException(msg); 70 } 71 else{ 72 System.out.println(msg); 73 return; 74 } 75 } 76 try{ 77 if ( outfile.createNewFile() ){ 79 System.out.println("created file " + outfile); 80 } 81 FileInputStream fis = new FileInputStream(infile); 83 FileOutputStream fos = new FileOutputStream(outfile.getPath(), true); 84 byte[] buff = new byte[1024]; 85 int read; 86 while ( (read = fis.read(buff)) > 0 ){ 87 fos.write(buff, 0, read); 88 } 89 fis.close(); 90 fos.flush(); 91 fos.close(); 92 System.out.println("appended " + infile 94 + " to " + outfile); 95 } 96 catch (IOException e){ 97 throw new BuildException(e); 98 } 99 } 100 101 104 public void setFile(File in){ 105 infile = in; 106 } 107 108 111 public void setAppendto(File out){ 112 outfile = out; 113 } 114 115 118 public void setFailonerror(boolean newValue){ 119 failonerror = newValue; 120 } 121 } 122 | Popular Tags |