In Java, I have text from a text field in a String variable called \"text\".
How can I save the contents of the \"text\" variable to a file?
Using Java 7:
Java 7
public static void writeToFile(String text, String targetFilePath) throws IOException { Path targetPath = Paths.get(targetFilePath); byte[] bytes = text.getBytes(StandardCharsets.UTF_8); Files.write(targetPath, bytes, StandardOpenOption.CREATE); }