1 8 package org.jivesoftware.util.log.output.io; 9 10 import org.jivesoftware.util.log.format.Formatter; 11 import java.io.File ; 12 import java.io.FileOutputStream ; 13 import java.io.IOException ; 14 15 20 public class FileTarget extends StreamTarget { 21 22 private File m_file; 24 25 private boolean m_append; 27 28 36 public FileTarget(final File file, final boolean append, final Formatter formatter) 37 throws IOException { 38 super(null, formatter); 39 40 if (null != file) { 41 setFile(file, append); 42 openFile(); 43 } 44 } 45 46 53 protected synchronized void setFile(final File file, final boolean append) 54 throws IOException { 55 if (null == file) { 56 throw new NullPointerException ("file property must not be null"); 57 } 58 59 if (isOpen()) { 60 throw new IOException ("target must be closed before " + 61 "file property can be set"); 62 } 63 64 m_append = append; 65 m_file = file; 66 } 67 68 73 protected synchronized void openFile() 74 throws IOException { 75 if (isOpen()) close(); 76 77 final File file = getFile().getCanonicalFile(); 78 79 final File parent = file.getParentFile(); 80 if (null != parent && !parent.exists()) { 81 parent.mkdir(); 82 } 83 84 final FileOutputStream outputStream = 85 new FileOutputStream (file.getPath(), m_append); 86 87 setOutputStream(outputStream); 88 open(); 89 } 90 91 97 protected synchronized File getFile() { 98 return m_file; 99 } 100 } 101 | Popular Tags |