⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filelocator.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    *    * @param      parent Path to the file, can be null    * @param      fileName  Name of file    * @return     data from the file    * @exception  XmlBlasterException    *             if the file is not readable or any error occurred while reading the file.    */   public static final byte[] readFile(String parent, String fileName)      throws XmlBlasterException   {      File f = (parent==null) ? new File(fileName) : new File(parent, fileName);      return readFile(f);   }      public static final byte[] readFile(File f)      throws XmlBlasterException   {      byte[] fileBlob = null;      String fileName = f.getAbsolutePath();      if (!f.exists()) {         throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME, "Sorry, can't find file " + fileName);      }      if (!f.isFile()) {         throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME, "Sorry, doesn't seem to be a file " + fileName);      }      if (!f.canRead()) {         throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME, "Sorry, no access permissions for file " + fileName);      }      FileInputStream from = null;      try {         from = new FileInputStream(f);         fileBlob = new byte[ (int) f.length()];         int bytes_read = from.read(fileBlob);         if (bytes_read != f.length()) {            throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME, "File read error in " + fileName + ": Excpected " + f.length() + " bytes, but only found " + bytes_read + "bytes");         }      }      catch (FileNotFoundException e) {         throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME, e.toString());      }      catch (IOException e2) {         throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME, e2.toString());      }      finally {         if (from != null)            try {            from.close();         }         catch (IOException e) {            ;         }      }      if (f.length() != fileBlob.length) {         throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME, "Read file " + fileName + " with size=" + f.length() + " but only got " + fileBlob.length + " bytes");      }      return fileBlob;   }   /**    * Write data from <code>byte[]</code> into a file.    *    * @param      parent the path, can be null    * @param      child the name    * @param      arr data    */   public static final void writeFile(String parent, String child, byte[] arr)      throws XmlBlasterException   {      try {         File to_file = (parent==null) ? new File(child) : new File(parent, child);         FileOutputStream to = new FileOutputStream(to_file);         to.write(arr);         to.close();      }      catch (Exception e) {         throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE_CONFIGURATION, ME, "Can't write file " + child + ":" + e.toString());      }   }   /**   * Write data from <code>StringBuffer</code> into a file.   * @param outName  name of file including path   * @param str      data   */   public static final void writeFile(String name, String str) throws XmlBlasterException {      writeFile(null, name, str.getBytes());   }   /**    * Write data from <code>StringBuffer</code> into a file.    * @param outName  name of file including path    * @param str      some binary data    */    public static final void writeFile(String name, byte[] arr) throws XmlBlasterException {       writeFile(null, name, arr);    }   /**   * Append data from into a file.   * @param outName  name of file including path   * @param str      Text   */   public static final void appendToFile(String outName, String str) throws XmlBlasterException {      try {         boolean append = true;         FileOutputStream to = new FileOutputStream(outName, append);         to.write(str.getBytes());         to.close();      }      catch (Exception e) {         throw new XmlBlasterException(Global.instance(), ErrorCode.RESOURCE, ME, "Can't write file " + e.toString());      }   }   /**   * Read a file into <code>String</code>.   * @param fileName Complete name of file   * @return ASCII data from the file<br />   *         null on error   */   public static final String readAsciiFile(String fileName) throws XmlBlasterException {      return readAsciiFile(null, fileName);   }   /**   * Read a file into <code>String</code>.   * <br><b>Example:</b><br>   *    <code>String data=FileUtil.readAsciiFile("/tmp/hello");</code>   * @param parent Path to the file   * @param fileName name of file   * @return ASCII data from the file<br />   *         null on error   */   public static final String readAsciiFile(String parent, String child) throws XmlBlasterException {      byte[] bb = readFile(parent, child);      if (bb == null)         return null;      return new String(bb);   }   /**    * Read a file into <code>byte[]</code>.    *    * @param      fileName    *             Complete name of file    * @return     data from the file    * @exception  JUtilsException    *             if the file is not readable or any error occured while reading the file.    */   public static final byte[] readFile(String fileName)      throws XmlBlasterException   {      return readFile(null, fileName);   }   public static final void deleteFile(String parent, String fileName) {      File f = new File(parent, fileName);      if (f.exists())         f.delete();   }    /**     * Concatenate a filename to a path (DOS and UNIX, checks for separator).     * @param path for example "/tmp"     * @param name for example "hello.txt"     * @return "/tmp/hello.txt"     */   public static String concatPath(String path, String name) {      if (path == null)         return name;      if (name == null)         return path;      if (path.endsWith(File.separator) && name.startsWith(File.separator))         return path + name.substring(1);      if (path.endsWith(File.separator))         return path + name;      if (name.startsWith(File.separator))         return path + name;      return path + File.separator + name;   }   /**   * Return the file name extension.   * @param fileName for example "/tmp/hello.txt"   * @return extension of the filename "txt"   */   public static String getExtension(String fileName) {      if (fileName == null)         return null;      int dot = fileName.lastIndexOf(".");      if (dot == -1)         return null;      return fileName.substring(dot + 1);   }   /**   * Strip the path and the file name extension.   * @param fileName for example "/tmp/hello.txt"   * @return filename without extension "hello"   */   public static String getBody(String fileName) {      if (fileName == null)         return null;      int dot = fileName.lastIndexOf(".");      String body = null;      if (dot == -1)         body = fileName;      else         body = fileName.substring(0, dot);      int sep = body.lastIndexOf(File.separator);      if (sep == -1)         return body;      return body.substring(sep + 1);   }      /**    * Deletes all files and subdirectories under dir.    * Returns true if all deletions were successful.    * If a deletion fails, the method stops attempting to delete and returns false.    * Thanks to "The Java Developers Almanac 1.4"    */   public static boolean deleteDir(File dir) {       if (dir.isDirectory()) {           String[] children = dir.list();           for (int i=0; i<children.length; i++) {               boolean success = deleteDir(new File(dir, children[i]));               if (!success) {                   return false;               }           }       }          // The directory is now empty so delete it       return dir.delete();   }      /**   * Convert some file extensions to MIME types.   * <p />   * A candidate for a property file like /etc/httpd/mime.types   * @param extension for example "xml"   * @param defaultVal for example "text/plain"   * @return for example "text/xml"   */   public static String extensionToMime(String extension, String defaultVal) {      if (extension == null)         return defaultVal;      if (extension.equalsIgnoreCase("xml"))         return "text/xml";      if (extension.equalsIgnoreCase("html"))         return "text/html";      if (extension.equalsIgnoreCase("gml"))         return "text/gml"; // grafic markup language http://infosun.fmi.uni-passau.de/Graphlet/GML      if (extension.equalsIgnoreCase("sgml"))         return "text/sgml";      if (extension.equalsIgnoreCase("gif"))         return "image/gif";      if (extension.equalsIgnoreCase("png"))         return "image/png";      if (extension.equalsIgnoreCase("jpeg"))         return "image/jpeg";      if (extension.equalsIgnoreCase("jpg"))         return "image/jpg";      if (extension.equalsIgnoreCase("pdf"))         return "application/pdf";      if (extension.equalsIgnoreCase("rtf"))         return "text/rtf";      return defaultVal;   }   /**    * java org.xmlBlaster.util.FileLocator -pluginsFile http://www.xmlblaster.org/empty.html     * @param args    */   public static void main(String[] args) {       Global glob = Global.instance();       glob.init(args);       FileLocator locator = new FileLocator(glob);       try {          URL url = locator.findFileInXmlBlasterSearchPath("pluginsFile", "xmlBlasterPlugins.xml");          if (url != null && url.getFile() != null) {             System.out.println("The file 'xmlBlasterPlugins.xml' has been found");             System.out.println("Its complete path is: '" + url.toString() + "' and the file is '" + url.getFile() + "'");             System.out.println("DUMP:");             System.out.println(locator.read(url));          }          else {             System.out.println("The file 'xmlBlasterPlugins.xml' has not been found");          }       }       catch (Exception ex) {          System.err.println("Error occured: " + ex.toString());       }    }   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -