
Apache Commons IO - Utility Classes
- Apache Commons IO - IOUtils
- Apache Commons IO - FileUtils
- Apache Commons IO - FilenameUtils
- Apache Commons IO - FileSystemUtils
- Apache Commons IO - IOCase
- Apache Commons IO - LineIterator
Apache Commons IO - Filter Classes
- Apache Commons IO - NameFileFilter
- Apache Commons IO - WildcardFileFilter
- Apache Commons IO - SuffixFileFilter
- Apache Commons IO - PrefixFileFilter
- Apache Commons IO - OrFileFilter
- Apache Commons IO - AndFileFilter
- Apache Commons IO - FileEntry
Apache Commons IO - Comparator Classes
- Apache Commons IO - NameFileComparator
- Apache Commons IO - SizeFileComparator
- LastModifiedFileComparator
Apache Commons IO - Stream Classes
Apache Commons IO - Useful Resources
Apache Commons IO - IOUtils Class
Overview
IOUtils class provide utility methods for reading, writing and copying files. The methods work with InputStream, OutputStream, Reader and Writer.
Class Declaration
Following is the declaration for org.apache.commons.io.IOUtils Class −
public class IOUtils extends Object
Features of IOUtils
The features of IOUtils are given below −
Provides static utility methods for input/output operations.
toXXX() − reads data from a stream.
write() − write data to a stream.
copy() − copy all data to a stream to another stream.
contentEquals − compare the contents of two streams.
Here is the input file we need to parse −
input.txt
Welcome to TutorialsPoint. Simply Easy Learning.
Example - Convert Stream to String using IOUtils
CommonsIoTester.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; public class CommonsIoTester { public static void main(String[] args) throws IOException { try(InputStream in = new FileInputStream("input.txt")) { String data = IOUtils.toString( in , "UTF-8"); System.out.println(data); } } }
Output
It will print the following result −
Welcome to TutorialsPoint. Simply Easy Learning.
Example - Transfer data using IOUtils
CommonsIoTester.java
package com.tutorialspoint; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class CommonsIoTester { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream("input.txt"); FileOutputStream fos = new FileOutputStream("output.txt")) { IOUtils.copy(fis, fos); System.out.println("Data transferred successfully."); } catch (IOException e) { e.printStackTrace(); } } }
Output
It will print the following result −
Data transferred successfully.
You can check the content of output.txt file created in current directory as −
Welcome to TutorialsPoint. Simply Easy Learning.
Example - Writing String to Stream using IOUtils
CommonsIoTester.java
package com.tutorialspoint; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; public class CommonsIoTester { public static void main(String[] args) throws IOException { try (FileWriter writer = new FileWriter("output.txt")) { IOUtils.write("Welcome to Tutorialspoint.", writer); IOUtils.write(System.lineSeparator(), writer); IOUtils.write("Simple Easy Learning.", writer); System.out.println("Content written to file."); } catch (IOException e) { e.printStackTrace(); } } }
Output
It will print the following result −
Content written to file.
You can check the content of output.txt file created in current directory as −
Welcome to Tutorialspoint. Simple Easy Learning.