Here you can find the source of readFile(Class> cls, String filename)
public static String readFile(Class<?> cls, String filename) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Main { public static String readFile(Class<?> cls, String filename) throws IOException { InputStream stream = cls.getResourceAsStream(filename); if (stream != null) { StringBuffer fileData = new StringBuffer(); BufferedReader reader = new BufferedReader( new InputStreamReader(stream)); char[] buf = new char[1024]; for (int bufLen = reader.read(buf); bufLen != -1; bufLen = reader .read(buf)) {/* w ww . ja v a 2 s .co m*/ String readData = String.valueOf(buf, 0, bufLen); fileData.append(readData); buf = new char[1024]; } reader.close(); return fileData.toString(); } throw new IOException("file cannot be found: " + filename); } }