I am using the below method to load a string from file to variable.
private static String readFile(String path) throws IOException {
FileInputStream stream = new FileInputStream(new File(path));
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder. */
return Charset.defaultCharset().decode(bb).toString();
}
finally {
stream.close();
}
}
The problem is that my variable has escape characters in it. I want my variables to contain:
some string
but instead it looks like:
some string
how can i improve my method to not allow that?
You can use Reader instead, and BufferedReader in particular to read lines from TXT file:
If you want to read the whole file, there are lots of utility classes that provide this functionality (like Google Guava):