Read File to String with Java

On March 29, 2013, in Java, Programming, by LuCuS

Read file to string with Java.

	public static String readFile(String filename) {
		StringBuilder sb = new StringBuilder();
		try
		{
	        FileReader fileReader = new FileReader(filename);
	        BufferedReader bufferedReader = new BufferedReader(fileReader);
	        String line = null;
	        while ((line = bufferedReader.readLine()) != null) {
	            sb.append(line + "\n");
	        }
	        bufferedReader.close();
	        fileReader.close();
		} catch(Exception e) { e.printStackTrace(); }
        return sb.toString();
    }

Thank you for your interest in my site. If you find the information provided on this site useful, please consider making a donation to help continue development!

PayPal will open in a new tab.
$2.00
$5.00
Other

Related Posts

Tagged with:  

Leave a Reply