FileInputStream fis = new FileInputStream(new File(“C:\\fileName.txt”));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.
int avail = fis.available();
byte[] bytes = new byte[avail];
fis.read(bytes, 0, avail);
String rfResponse = new String(bytes);
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.
better way of comparing Strings
Lets say there is some String variable, to be compared with some static value in the program,
what generally developers do is to explicitly check on 'nulls' and use equalsIgnoreCase() to compare with that static String.
For example, consider varString is of type String and is to be compared with the static value "Success". Generally to check whether the varString is 'Success' or not, I see people coding it like
Just try....
what generally developers do is to explicitly check on 'nulls' and use equalsIgnoreCase() to compare with that static String.
For example, consider varString is of type String and is to be compared with the static value "Success". Generally to check whether the varString is 'Success' or not, I see people coding it like
if(varString != null && varString.equalsIgnoreCase("Success")) ...but there is another better way of doing this, that is simply write
if("Success".equalsIgnoreCase(varString)) ...the above statement includes the check on nulls and the String value at once. No one claims there could be performance uplift due to this statement, but code looks credible and simple.
Just try....
Subscribe to:
Comments (Atom)