how to write text to file in java

www.lautt‮.iru‬com
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();
        }
    }
}
Created Time:2017-09-15 17:17:27  Author:lautturi