What's the simplest way to create and write to a (text) file in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
In Java 7 and up:
There are useful utilities for that though:
Note also that you can use a
FileWriter
, but it uses the default encoding, which is often a bad idea - it's best to specify the encoding explicitly.Below is the original, prior-to-Java 7 answer
See also: Reading, Writing, and Creating Files (includes NIO2).
Since the author did not specify whether they require a solution for Java versions that have been EoL'd (by both Sun and IBM, and these are technically the most widespread JVMs), and due to the fact that most people seem to have answered the author's question before it was specified that it is a text (non-binary) file, I have decided to provide my answer.
First of all, Java 6 has generally reached end of life, and since the author did not specify he needs legacy compatibility, I guess it automatically means Java 7 or above (Java 7 is not yet EoL'd by IBM). So, we can look right at the file I/O tutorial: https://docs.oracle.com/javase/tutorial/essential/io/legacy.html
Oh well, that rules out java.io.File. If a file cannot be written/appended, you may not be able to even know why.
We can continue looking at the tutorial: https://docs.oracle.com/javase/tutorial/essential/io/file.html#common
If you have all lines you will write (append) to the text file in advance, the recommended approach is https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#write-java.nio.file.Path-java.lang.Iterable-java.nio.charset.Charset-java.nio.file.OpenOption...-
Here's an example (simplified):
Another example (append):
If you want to write file content as you go: https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#newBufferedWriter-java.nio.file.Path-java.nio.charset.Charset-java.nio.file.OpenOption...-
Simplified example (Java 8 or up):
Another example (append):
These methods require minimal effort on the author's part and should be preferred to all others when writing to [text] files.
best way is to use Java7: Java 7 introduces a new way of working with the filesystem, along with a new utility class – Files. Using the Files class, we can create, move, copy, delete files and directories as well; it also can be used to read and write to a file.
Write with FileChannel If you are dealing with large files, FileChannel can be faster than standard IO. The following code write String to a file using FileChannel:
Write with DataOutputStream
Write with FileOutputStream
Let’s now see how we can use FileOutputStream to write binary data to a file. The following code converts a String int bytes and writes the bytes to file using a FileOutputStream:
Write with PrintWriter we can use a PrintWriter to write formatted text to a file:
Write with BufferedWriter: use BufferedWriter to write a String to a new file:
append a String to the existing file:
Use:
Using
try()
will close stream automatically. This version is short, fast (buffered) and enables choosing encoding.This feature was introduced in Java 7.
Just include this package:
And then you can use this code to write the file:
One line only !
path
andline
are Strings