how to write to a txt file in java/**
* @author lautturi.com
* Java example: write a txt file in java
*/
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.*;
public class Lautturi {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
// If the file does not exist, create one
// Overwrite if file exists
PrintWriter writer = new PrintWriter("test.txt", "UTF-8");
writer.println("The first line");
writer.println("The second line");
writer.close();
}
}