1 package com.zbmon.util;
   2 
   3 import java.io.File;
   4 import javax.swing.JFileChooser;
   5 import javax.swing.filechooser.FileFilter;
   6 
   7 /**
   8  * Various static utility methods.
   9  *
  10  * @version $Rev: 222 $
  11  * @modify KIM, KYUNG IL
  12  */
  13 public class FileUtil {
  14   public final static String net = "net";
  15   public final static String kif = "kif";
  16   public final static String rdf = "rdf";
  17   public final static String rdfs = "rdfs";
  18   public final static String owl = "owl";
  19   public final static String xml = "xml";
  20   public final static String clu = "clu";
  21 
  22   /**
  23    * File filter for Pajek network files.
  24    */
  25   public static final FileFilter NETWORK_FILE_FILTER = new FileFilter() {
  26     public boolean accept(File file) {
  27       if (file.isDirectory()) {
  28         return true;
  29       }
  30       else if (getFileExtension(file).equals(net)) {
  31         return true;
  32       }
  33       else {
  34         return false;
  35       }
  36     }
  37 
  38     public String getDescription() {
  39       return "Pajek Network Files (*.net)";
  40     }
  41   };
  42 
  43   /**
  44    * File filter for KIF files.
  45    */
  46   public static final FileFilter KIF_FILE_FILTER = new FileFilter() {
  47     public boolean accept(File file) {
  48       if (file.isDirectory()) {
  49         return true;
  50       }
  51       else if (getFileExtension(file).equals(kif)) {
  52         return true;
  53       }
  54       else {
  55         return false;
  56       }
  57     }
  58 
  59     public String getDescription() {
  60       return "KIF Files (*.kif)";
  61     }
  62   };
  63 
  64   /**
  65    * File filter for ontology files (XML, RDF(S) and OWL files).
  66    */
  67   public static final FileFilter ONTOLOGY_FILE_FILTER = new FileFilter() {
  68     public boolean accept(File file) {
  69       if (file.isDirectory()) {
  70         return true;
  71       }
  72       else if (getFileExtension(file).equals(rdf)
  73           || getFileExtension(file).equals(rdfs)
  74           || getFileExtension(file).equals(owl)
  75           || getFileExtension(file).equals(xml)) {
  76         return true;
  77       }
  78       else {
  79         return false;
  80       }
  81     }
  82 
  83     public String getDescription() {
  84       return "Ontology Files (*.rdf; *.rdfs; *.owl; *.xml)";
  85     }
  86   };
  87 
  88   /**
  89    * File filter for cluster index files (*.clu).
  90    */
  91   public static final FileFilter CLUSTER_INDEX_FILE_FILTER = new FileFilter() {
  92     public boolean accept(File file) {
  93       if (file.isDirectory()) {
  94         return true;
  95       }
  96       else if (getFileExtension(file).equals(clu)) {
  97         return true;
  98       }
  99       else {
 100         return false;
 101       }
 102     }
 103 
 104     public String getDescription() {
 105       return "Cluster Index Files (*.clu)";
 106     }
 107   };
 108 
 109   private FileUtil() {
 110   }
 111 
 112   /**
 113    * Selects the specified file (or the first existing parent directory) in a
 114    * file chooser.
 115    *
 116    * @param file
 117    *            a file
 118    */
 119   public static void selectFile(JFileChooser chooser, File file) {
 120     if (file.isFile()) {
 121       chooser.setSelectedFile(file);
 122     }
 123     else if (file.isDirectory()) {
 124       chooser.setCurrentDirectory(file);
 125     }
 126     else {
 127       while (file != null && !file.isDirectory()) {
 128         file = file.getParentFile();
 129       }
 130       if (file != null) {
 131         chooser.setCurrentDirectory(file);
 132       }
 133     }
 134   }
 135 
 136   /**
 137    * Gets the extension in lowercase of the specified file.
 138    *
 139    * @param file
 140    *            a file
 141    */
 142   public static String getFileExtension(File file) {
 143     return getFileExtension(file.getName());
 144   }
 145 
 146   /**
 147    * Gets the extension in lowercase of the specified file.
 148    *
 149    * @param filename
 150    *            a filename
 151    */
 152   public static String getFileExtension(String filename) {
 153     String extension = "";
 154     int i = filename.lastIndexOf('.');
 155     if (i > 0 && i < filename.length() - 1) {
 156       extension = filename.substring(i + 1).toLowerCase();
 157     }
 158     return extension;
 159   }
 160 }


CategoryJava

ZbmonWiki: FileUtil.java (2005-11-07 14:25:42에 zbmon가(이) 마지막으로 수정)