Android Text File Read readStringFromClasspath(String path, Class c)

Here you can find the source of readStringFromClasspath(String path, Class c)

Description

Reads the entire InputStream and returns its content as a single String

License

Open Source License

Parameter

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

Exception

Parameter Description
IOException an exception

Return

the file content as String

Declaration

public static String readStringFromClasspath(String path, Class c)
        throws IOException 

Method Source Code

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.URL;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

public class Main{
    /**/*from www. j ava 2 s  . 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"));
    }
}

Related

  1. readFileLineByLine(File file)
  2. readFree(File file)
  3. readFromFile(File file)
  4. readFull(String path)
  5. readLines(String file)
  6. readStringFromFile(String fileName)
  7. readText(String path)
  8. readTextFile(String filePath)
  9. readTextFile(String path)