how to write into a file java code example
Example 1: Java program to write to text file
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class WriteIntoTextFile
{
public static void main(String[] args)
{
try
{
FileWriter fw = new FileWriter("B:\\demo.txt");
fw.write("Hello World");
fw.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
System.out.println("Successful");
}
}
Example 2: java create txt file
File myObj = new File("path");
myObj.createNewFile();
FileWriter myWriter = new FileWriter("path");
myWriter.write("text");
myWriter.close();