Here you can find the source of readStringFromClasspath(String path, Class c)
Parameter | Description |
---|---|
path | the loacation of the file on the CLASSPATH (e.g. /com/gooddata/processor/COMMANDS.txt) |
c | Class for determining the Java classloader |
Parameter | Description |
---|---|
IOException | an exception |
public static String readStringFromClasspath(String path, Class c) throws IOException
//package com.java2s; import java.io.*; import java.net.URL; public class Main { /**/* ww w .j a va2s .co m*/ * Reads the entire {@link InputStream} and returns its content as a single {@link String} * * @param path the loacation of the file on the CLASSPATH (e.g. /com/gooddata/processor/COMMANDS.txt) * @param c Class for determining the Java classloader * @return the file content as String * @throws IOException */ public static String readStringFromClasspath(String path, Class c) throws IOException { final InputStream is = c.getResourceAsStream(path); return readStringFromBufferedReader(createBufferedUtf8Reader(is)); } /** * Reads all content from the given {@link Reader} and returns it as a single {@link String} * * @param br the file * @return the file content as String * @throws IOException */ private static String readStringFromBufferedReader(BufferedReader br) throws IOException { StringBuffer sbr = new StringBuffer(); for (String ln = br.readLine(); ln != null; ln = br.readLine()) sbr.append(ln + "\n"); br.close(); return sbr.toString(); } /** * Opens a file given by a path and returns its {@link BufferedReader} using the * UTF-8 encoding * * @param path path to a file to be read * @return UTF8 BufferedReader of the file <tt>path</tt> * @throws IOException */ public static BufferedReader createBufferedUtf8Reader(String path) throws IOException { return createBufferedUtf8Reader(new File(path)); } /** * Opens a file given by a path and returns its {@link BufferedReader} using the * UTF-8 encoding * * @param file file to be read * @return UTF8 BufferedReader of the <tt>file</tt> * @throws IOException */ public static BufferedReader createBufferedUtf8Reader(File file) throws IOException { return createBufferedUtf8Reader(new FileInputStream(file)); } /** * Opens a URL and returns its {@link BufferedReader} using the UTF-8 encoding * * @param url to be read * @return UTF8 BufferedReader of the <tt>url</tt> * @throws IOException */ public static BufferedReader createBufferedUtf8Reader(URL url) throws IOException { return createBufferedUtf8Reader(url.openStream()); } /** * Creates a {@link BufferedReader} on the top of the given {@link InputStream} using the * UTF-8 encoding * * @param is file to be read * @return UTF8 BufferedReader of the <tt>file</tt> * @throws IOException */ public static BufferedReader createBufferedUtf8Reader(InputStream is) throws IOException { return new BufferedReader(new InputStreamReader(is, "utf8")); } }