Java BufferedReader Read loadTemplate(final String resource)

Here you can find the source of loadTemplate(final String resource)

Description

Loads the specified resource file and returns it as String.

License

Open Source License

Parameter

Parameter Description
resource Path to the Java resource.

Exception

Parameter Description
InterruptedException If the thread is interrupted.
IOException If there is a problem with reading.

Return

String content of the resource file.

Declaration

public static String loadTemplate(final String resource) throws IOException, InterruptedException 

Method Source Code


//package com.java2s;
/*/*from   w  ww  .ja v  a 2 s .  c om*/
 *  Copyright (C) 2010 vektor
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.BufferedReader;

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

import java.io.Reader;

public class Main {
    /**
     * Loads the specified resource file and returns it as String.
     *
     * @param resource Path to the Java resource.
     * @return String content of the resource file.
     *
     * @throws InterruptedException If the thread is interrupted.
     * @throws IOException If there is a problem with reading.
     */
    public static String loadTemplate(final String resource) throws IOException, InterruptedException {
        final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
        final StringBuilder ret = new StringBuilder();
        readerToBuilder(new InputStreamReader(is), ret);
        return ret.toString();
    }

    /**
     * Reads the specified {@link Reader} and writes its content to the specified
     * {@link StringBuilder}.
     *
     * @param r Reader to read from.
     * @param sb String buffer to write to.
     * 
     * @throws InterruptedException If the thread is interrupted.
     * @throws IOException If there is a problem with reading.
     */
    public static void readerToBuilder(final Reader r, final StringBuilder sb)
            throws InterruptedException, IOException {
        final BufferedReader reader = new BufferedReader(r);

        String line = reader.readLine();
        while (line != null) {
            sb.append(line).append('\n');
            if (Thread.interrupted()) {
                throw new InterruptedException();
            }
            line = reader.readLine();
        }
    }
}

Related

  1. loadSQLFromFile(String fileName)
  2. loadStream(InputStream is)
  3. loadStreamContent(InputStream stream)
  4. loadSystemClassPath()
  5. loadTableList(List list, String fileName)
  6. loadTemplate(String templateUrl)
  7. loadTemplateParametersFile(File f)
  8. loadTestFixture(String path)
  9. loadTestUserEventRelation(String eventFilePath, HashMap> TestUser2EventIndexSetMap)