You are on page 1of 1

import java.io.

File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFileExample {


public static void main(String[] args) {
String fileName = "secretary.txt";
String content = "you are my friend, let's work together";

try {
File file = new File(fileName);

// Create the file if it doesn't exist


if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
}

// Write the content to the file


FileWriter writer = new FileWriter(file);
writer.write(content);
writer.close();
System.out.println("Content written to the file.");
} catch (IOException e) {
System.err.println("An error occurred.");
e.printStackTrace();
}
}
}

You might also like