Like reading, we have a lot of ways to write to file, but this one is better


File file = new File("C:\myfile.txt");
try {
if(!file.exists()){
file.createNewFile();
}
PrintWriter out = new PrintWriter(file);
out.println("String will be in file!");
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}