You may use java.nio.file.Files which is available from Java 7 to write contents to a file in runtime. The file will be created if you give a correct path also you can set the preferred encoding.
JAVA 7+
Edited after suggestion of @Ivan
You may use PrintWriter, BufferedWriter, FileUtils etc. there are many ways. I am sharing an example with Files
String encodedString = "some higly secret text";
Path filePath = Paths.get("file.txt");
try {
Files.write(filePath, encodedString, Charset.forName("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
System.out.println("unable to write to file, reason"+e.getMessage());
}
To write multiple lines
List<String> linesToWrite = new ArrayList<>();
linesToWrite.add("encodedString 1");
linesToWrite.add("encodedString 2");
linesToWrite.add("encodedString 3");
Path filePath = Paths.get("file.txt");
try {
Files.write(filePath, linesToWrite, Charset.forName("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
System.out.println("unable to write to file, reason"+e.getMessage());
}
There are a million other ways but I think it would be good to start with because of its simplicity.
Before Java 7
PrintWriter writer = null;
String encodedString = "some higly secret
try {
writer = new PrintWriter("file.txt", "UTF-8");
writer.println(encodedString);
// to write multiple: writer.println("new line")
} catch (FileNotFoundException | UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
writer.close();
}