The temporary file is just a regular file created on a predefined directory. In Java, we can use the NIO Files.write() to write data to a temporary file. In Java, there are many ways to write data or text to a temporary file; it works like writing to a regular file. You can choose BufferedWriter, FileWriter etc, but in most cases, the NIO java.nio.Files should be enough to write or append data to a file.
This article shows a few ways to save a byte into a file. For JDK 1.7 and above, the NIO Files.write is the simplest solution to save byte to a file. FileOutputStream is the best alternative. If we have Apache Commons IO, try FileUtils.
In Java, we can use Files.readAllBytes(path) to convert a File object into a byte. Before Java 7, we can initiate a new byte with a predefined size (same with the file length), and use FileInputStream to read the file data into the new byte. We also can use FileUtils from Apache Commons IO to read the file.
This article shows how to use the following Java APIs to append text to the end of a file. (1) Files.write – Append a single line to a file, Java 7. (2) Files.write – Append multiple lines to a file, Java 7, Java 8. (3) Files.writeString – Java 11. (4) FileWriter, (5) FileOutputStreamFileUtils – Apache Commons IO. In Java, for NIO APIs like Files.write, we can use StandardOpenOption.APPEND to enable the append mode. For classic IO APIs like FileWriter or FileOutputStream, we can pass a true to the constructor’s second argument to enable the append mode.
In Java, we can use Files.write to create and write to a file. The Files.write also accepts an Iterable interface; it means this API can write a List to a file. Before Java 7, for writing bytes (image) to a file, we use FileOutputStream; for writing characters (text) to a file, we use FileWriter, and usually wrapped by a BufferedWriter to gain performance. In Java 7, there is a new NIO class named java.nio.file.Files, and we can use Files.write() to write both bytes and characters. In Java 8, we can use Files.newBufferedWriter(path) to create a BufferedWriter. In Java 11, there is a new Files.writeString API to write string directly to a file.
In Java, we can use BufferedWriter to write content into a file, safety with try-resources in Java 7+. If possible, uses Files.write instead, one line, simple and nice. For Append mode, pass a true as second argument in FileWriter. Before the JDK 7 try-resources, we need to handle the close() manually.
In Java, FileOutputStream is a bytes stream class that’s used to handle raw binary data. To write the data to file, you have to convert the data into bytes and save it to file. An updated JDK7 example, using new “try resource close” method to handle file easily.
In Java, the OutputStreamWriter accepts a charset to encode the character streams into byte streams. We can pass a StandardCharsets.UTF_8 into the OutputStreamWriter constructor to write data to a UTF-8 file. In Java 7+, many File I/O and NIO writers start to accept charset as an argument, making write data to a UTF-8 file very easy.
In Java, there are many ways to create and write to a file: using Files.newBufferedWriter (Java 8), using Files.write (Java 7), using PrintWriter, using File.createNewFile