URL url = Resources.getResource("myFile.txt");
File myFile = new File(url.toURI());
String content = FileUtils.readFileToString(myFile, "UTF-8"); // or any other encoding
@Hamidan, choosing the right encoding is a very complex algorithm. It is often implemented in text editor but they sometimes fail to detect the correct encoding. I would not expect a file reading API to embed such complex algorithm to read my file.
@SecretService Also, those algorithms make use of information like the operating system's language, locale and other regional settings which means that reading a file without specifying an encoding may work on your setup but not on someone else's.
Sorry for downvoting, but I don't like answers that add big dependencies to trivial problems.
Why do one have to specify the encoding, i don't get that. If I read the file, i just want what is in it, it should figure out what encoding it is like my editor does. When I open in Notepad or ++, I dont tell it what encoding it should use. I am using this method and then afterwards writeStringToFile ... but the contents differ. I get strange tokens in the cloned file.. i don't get why I should have to specify an encoding.