Here you can find the source of getResource(Class c, String name)
Parameter | Description |
---|---|
c | The that defines where we look for the String. |
name | The name of the resource containing the string. |
public static String getResource(Class c, String name)
//package com.java2s; /*/*ww w . ja v a 2 s.c o m*/ * Copyright (C) 2010-2011 VTT Technical Research Centre of Finland. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ import java.io.InputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Scanner; public class Main { /** * Reads a string resource from the resource path (=classpath) of the given class. * * @param c The that defines where we look for the String. * @param name The name of the resource containing the string. * @return The String representation of the resource, newlines represented by '\n'. */ public static String getResource(Class c, String name) { InputStream is = c.getResourceAsStream(name); return getResource(is); } public static String getResource(InputStream in) { StringBuilder text = new StringBuilder(); try (Scanner scanner = new Scanner(in, "UTF-8")) { while (scanner.hasNextLine()) { String line = scanner.nextLine(); text.append(line); if (scanner.hasNextLine()) { text.append("\n"); } } } return text.toString(); } /** * Creates a string representation for a stack trace for an exception. * For logging, failure analysis, etc. * * @param t The throwable for which to create the string. * @return The stack trace as string. */ public static String toString(Throwable t) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); t.printStackTrace(pw); return sw.toString(); } }