Java Resource Load getResourceAsString(Class clazz, String name)

Here you can find the source of getResourceAsString(Class clazz, String name)

Description

get Resource As String

License

Open Source License

Declaration

public static String getResourceAsString(Class<?> clazz, String name) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Oobium, Inc./*from   w w  w  .  j  av  a  2s .com*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Jeremy Dowdall <jeremy@oobium.com> - initial API and implementation
 ******************************************************************************/

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

public class Main {
    public static String getResourceAsString(Class<?> clazz, String name) {
        return getString(clazz.getResourceAsStream(name));
    }

    public static String getString(InputStream in) {
        if (in != null) {
            try {
                StringBuilder sb = new StringBuilder();
                int c;
                while ((c = in.read()) != -1) {
                    sb.append((char) c);
                }
                return sb.toString();
            } catch (IOException e) {
                // discard
            }
        }
        return null;
    }
}

Related

  1. getResourceAsStream(String resource, ClassLoader classLoader)
  2. getResourceAsStream(String resource, ClassLoader classLoader)
  3. getResourceAsStream(String resourcePath)
  4. getResourceAsStreamInClassPath(@SuppressWarnings("rawtypes") Class clazz, String filepath)
  5. getResourceAsStrem(String name)
  6. getResourceAsString(Class clazz, String resource)
  7. getResourceAsString(Class theClass, String fileName)
  8. getResourceAsString(final String resourceName, final Class caller, final String charset)
  9. getResourceAsString(InputStream in)