I made a method that takes a File and a String. It replaces the file with a new file with that string as its contents.
File
String
This is what I made
You could look into Java's NIO capabilities. It may support what you want to do.
Java NIO FileChannel versus FileOutputstream performance / usefulness