DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Jean-pierre has posted 6 posts at DZone. View Full User Profile

Loading A Text File Into A String From The Classpath

  • submit to reddit
        Locate a text file in the classpath and read it into a String

     * @param filePath      name of file to open. The file can reside
     *                      anywhere in the classpath
    private String readFileAsString(String filePath) throws {
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new InputStreamReader(this
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            buf = new char[1024];
        return fileData.toString();