In this example, we read characters from the Reader and write them in the File using FileWriter.
3. Convert Reader to File using Guava library
The Guava library comes with a direct solution:
First, we convert Reader to Byte Array, and then we used Files.write(bytes, file) method to write all read bytes to the File.
4. Read from Reader and write to File using Apache Commons IO library
The Apache Commons IO library provides the simplest solution:
In this example first, we used IOUtils.toByteArray(..) method to convert Reader to byte[], and then we write that bytes to the File using another great utility - FileUtils.writeByteArrayToFile(..) method.
5. Conclusion
In this article, we presented various ways to convert Reader to a File in Java. The most transparent solution comes with Apache Commons IO library where we used two utility classes: IOUtils and FileUtils.
The examples used in this tutorial are available under our GitHub repository.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}