Java – Load text file to string

Veröffentlicht von

Most common in programming is to load text files to a string. Since this usually involves some lines of code, here is a snippet for that:

/**
 * Loads a file to String
 * @param fileName - file name for example "C:\\MeineFile.txt"
 * @return
 * @throws IOException 
 */
private String loadFileToString(String fileName) throws IOException {
    File file = new File(fileName);
    StringBuffer content = new StringBuffer();
    BufferedReader reader = null;

    try {
        reader = new BufferedReader(new FileReader(file));
        String s = null;

        while ((s = reader.readLine()) != null) {
            content.append(s).append(System.getProperty("line.separator"));
        }
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            throw e;
        }
    }   
    return content.toString();
}

Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.