前言:

    在工作当中我们往往遇到很多文件的操作,我们也习惯写一些自己定义的工具类来简化文件操作,其实apache的commons的FileUtils类就是这样一个工具类,使用它能大大的简化我们对文件的操作。

 

/**
 * 从文件系统读取一个资源,使用当前ClassLoader读取相对路径
 * @return 资源的字符串
 * @throws Exception 资源不存在
 */
@Override
public String load() throws Exception {
    String result = null;
    try {
        String path = this.url;
        if (this.fileName != null) {
            path = path + "/" + this.fileName;
        }
        URL pathUrl = FileLoader.class.getClassLoader().getResource(path);
        result = FileUtils.readFileToString(new File(pathUrl.getPath()), "utf-8");
    } catch (Exception e) {
        throw e;
    }
    return result;
}
View Code

相关文章: