Apache Commons IO Tutorial
Selected Reading
- Apache Commons IO - Discussion
- Apache Commons IO - Useful Resources
- Apache Commons IO - Quick Guide
- Apache Commons IO - TeeOutputStream
- Apache Commons IO - TeeInputStream
- LastModifiedFileComparator
- Apache Commons IO - SizeFileComparator
- Apache Commons IO - NameFileComparator
- Apache Commons IO - FileAlterationMonitor
- Apache Commons IO - FileAlterationObserver
- Apache Commons IO - FileEntry
- Apache Commons IO - AndFileFilter
- Apache Commons IO - OrFileFilter
- Apache Commons IO - PrefixFileFilter
- Apache Commons IO - SuffixFileFilter
- Apache Commons IO - WildcardFileFilter
- Apache Commons IO - NameFileFilter
- Apache Commons IO - LineIterator
- Apache Commons IO - IOCase
- Apache Commons IO - FileSystemUtils
- Apache Commons IO - FilenameUtils
- Apache Commons IO - FileUtils
- Apache Commons IO - IOUtils
- Apache Commons IO - Environment Setup
- Apache Commons IO - Overview
- Apache Commons IO - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Apache Commons IO - IOUtils
Apache Commons IO - IOUtils
IOUtils provide utipty 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 −
pubpc class IOUtils extends Object
Features of IOUtils
The features of IOUtils are given below −
Provides static utipty 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.
Example of IOUtils Class
Here is the input file we need to parse −
Welcome to TutorialsPoint. Simply Easy Learning.
IOTester.java
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.commons.io.IOUtils; pubpc class IOTester { pubpc static void main(String[] args) { try { //Using BufferedReader readUsingTraditionalWay(); //Using IOUtils readUsingIOUtils(); } catch(IOException e) { System.out.println(e.getMessage()); } } //reading a file using buffered reader pne by pne pubpc static void readUsingTraditionalWay() throws IOException { try(BufferedReader bufferReader = new BufferedReader( new InputStreamReader( new FileInputStream("input.txt") ) )) { String pne; while( ( pne = bufferReader.readLine() ) != null ) { System.out.println( pne ); } } } //reading a file using IOUtils in one go pubpc static void readUsingIOUtils() throws IOException { try(InputStream in = new FileInputStream("input.txt")) { System.out.println( IOUtils.toString( in , "UTF-8") ); } } }
Output
It will print the following result −
Welcome to TutorialsPoint. Simply Easy Learning. Welcome to TutorialsPoint. Simply Easy Learning.Advertisements