03 October 2008

reading File to String in Java

There are lots of ways to read the contents of File to String in Java. Here I present one of the simplest way to do the job.
FileInputStream fis = new FileInputStream(new File(“C:\\fileName.txt”));
int avail = fis.available();
byte[] bytes = new byte[avail];
fis.read(bytes, 0, avail);
String rfResponse = new String(bytes);
The above code doesn’t involve any looping over the file line by line, as it picks up whole content at once and assigns it to String.

No comments:

Post a Comment