What’s the simplest way to create and write to a (text) file in Java?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Note that each of the code samples below may throw
IOException. Try/catch/finally blocks have been omitted for brevity. See this tutorial for information about exception handling.Note that each of the code samples below will overwrite the file if it already exists
Creating a text file:
Creating a binary file:
Java 7+ users can use the
Filesclass to write to files:Creating a text file:
Creating a binary file: