Java Example - File Writing

Java Example

The following example demonstrates using the write() method to write content to a file:

Example

/* author by example.com Main.java */ import java.io.*; public class Main { public static void main(String[] args) { try { BufferedWriter out = new BufferedWriter(new FileWriter("example.txt")); out.write("Example"); out.close(); System.out.println("File created successfully!"); } catch (IOException e) { } } }

The output of running the above code is:

File created successfully!

After successful creation, a file named example.txt will be generated in the current directory, and the string "Example" will be written to the file.

Java Example

Other Extensions