Java InputStreamReader Read readFileFromResource(@SuppressWarnings("rawtypes") Class refClass, String filePath)

Here you can find the source of readFileFromResource(@SuppressWarnings("rawtypes") Class refClass, String filePath)

Description

Read a file from a resource rather than from a file

License

Open Source License

Parameter

Parameter Description
filePath name of file to open. The file can reside anywhere in the classpath

Declaration

public static String readFileFromResource(@SuppressWarnings("rawtypes") Class refClass, String filePath)
        throws java.io.IOException 

Method Source Code


//package com.java2s;
import java.io.*;

public class Main {
    /**/*from   w  w w  .java  2  s.c o m*/
     * Read a file from a resource rather than from a file
     *
     * @param filePath      name of file to open. The file can reside
     *                      anywhere in the classpath
     */
    public static String readFileFromResource(@SuppressWarnings("rawtypes") Class refClass, String filePath)
            throws java.io.IOException {
        StringBuffer fileData = new StringBuffer(1000);

        BufferedReader reader = new BufferedReader(new InputStreamReader(refClass.getResourceAsStream(filePath)));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    }
}

Related

  1. readFileCharacters(File file, boolean fix)
  2. readFileData(File file)
  3. readFileData(String directoryLocation, String fileName)
  4. readFileFromClassPath(String file)
  5. readFileFromDisk(String filepath)
  6. readFileFromResource(String filename)
  7. readFileIntoLines(File file)
  8. readFileIntoLinesOfLongs(File file)
  9. readFileLineByLine(final File filePath)