We suppose that we have a text and we want to save this text inside a file using Java. To do this we can use the PrintWriter , lets see the following solution :
PrintWriter out = new PrintWriter("filename.txt");
Then, write your String to it, just like you would to any output stream :
Be sure to call out.close() when you’ve finished writing to handling the exception.
Read also : How to get file extension in JAVA