开发者

Call a HTML File From Jar

Im generating a report from a template html file in my program. It resides in /src/main/resources and its name is "template.html". Im using ClassLoader inside the code like this:

   private String readTemplateFile() {
        String str = "";
        URL url = ClassLoader.getSystemResource("template.html");

        try {
            FileReader input = new FileReader(url.getFile());
            BufferedReader bufRead = new BufferedReader(input);
            String line;

            line = bufRead.readLine();
            str = line;
            while (line != null) {
                line = bufRead.readLine();
                str += line + "\n";

            }
            bufRead.close();

        } catch (IOException e) {
        }

        return str;
    }

It is doing nicely when you开发者_开发知识库 run the code inside IDE but when i make a runnable jar from it, it is generating an empty report. What is the solution? Thanks for reading.


If it's in a jar, it's no longer a file.

Use ClassLoader.getResourceAsStream() to retrieve the resource as an InputStream.

Then convert the InputStream to a String.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜