Are you looking for customized advices? Click here to contact the expert!
by
0
8
290k
1
Top 1% !
Popular
Pearl of Wisdom
Tagged
Easy-to-find
Specified
Refined
OpenSource
Popularity: 4016th place
Created
Modified Jan 10, 2017

Published on:

JVMjre6
LanguageJava
LicenseMIT_X11
OSLinux
OSWindows

How to read a file into a string in Java

This simple snippet reads a file and put the content into a returned string
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/429/How-to-read-a-file-into-a-string-in-Java?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
private static String readFileAsString(String filePath) throws java.io.IOException{ StringBuffer fileData = new StringBuffer(1000); BufferedReader reader = new BufferedReader(new FileReader(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(); }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus