*All the code I present here is taken from https://www.w3schools.com/java/java_files_create.asp I highly recommend checking it for more details.
First, you would need to create a file/ check if one exists,
import java.io.File; // Import the File class
import java.io.IOException; // Import the IOException class to handle errors
public class CreateFile {
public static void main(String[] args) {
try {
File myObj = new File("filename.txt");
if (myObj.createNewFile()) {
System.out.println("File created: " + myObj.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
After that you should be able to write in the file using the FileWriter class
FileWriter myWriter = new FileWriter("filename.txt");
and to write in a file you want to use the .write method
myWriter.write("a string");
and once you are done modifying the file using the close() method to close the file
myWriter.close();