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

📄 resourceutil.java

📁 struts+spring+hibernate自创框架
💻 JAVA
字号:
package com.pegasus.framework.util;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class ResourceUtil {
    /**
     * Private constructor to prevent instances of this class.
     */
    private ResourceUtil() {
    }

    /**
     * Return a resource on the classpath as a Stream object.
     *
     * @param resource the resource to find.
     * @return the resource as input stream.
     */
    public static InputStream getResourceAsStream(String resource) {
        InputStream in = null;
        ClassLoader loader = ResourceUtil.class.getClassLoader();
        if (loader != null) in = loader.getResourceAsStream(resource);
        if (in == null) in = ClassLoader.getSystemResourceAsStream(resource);
        if (in == null) throw new RuntimeException("Error locating resource " + resource);
        return in;
    }

    /**
     * * Read a file on the classpath by line, then combine them into a String.
     *
     * @param fileName the file to find.
     * @return a string of file.
     */
    public static String readFileAsString(String fileName) {
        try {
            InputStream in = getResourceAsStream(fileName);
            InputStreamReader inReader = new InputStreamReader(in, "GBK");
            BufferedReader reader = new BufferedReader(inReader);
            StringBuffer sb = new StringBuffer();
            String str = null;

            while ((str = reader.readLine()) != null) {
                sb.append(str);
            }

            reader.close();
            in.close();

            return sb.toString();

        } catch (IOException ioe) {
            throw new RuntimeException("Error reading file " + fileName, ioe);
        }
    }
}

⌨️ 快捷键说明

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