www.lautt.irucom
how to write text to file in java/**
* @author lautturi.com
* Java example: how to save a string to a text file
*/
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
public class Main
{
public static void main(String[] args)
{
Path filePath = Paths.get("F:/", "java", "test.txt");
try
{
//Write content to file
// Files.writeString(path, contents, StandardCharsets.UTF_8);
Files.writeString(filePath, "Hello World !!", StandardOpenOption.APPEND);
//Verify file content
String content = Files.readString(filePath);
System.out.println(content);
}
catch (IOException e)
{
// Handle exception
e.printStackTrace();
}
}
}