Apache Commons IO – LineIterator ”; Previous Next LineIterator provides a flexible way to work with a line-based file. Class Declaration Following is the declaration for org.apache.commons.io.LineIterator Class − public class LineIterator extends Object implements Iterator<String>, Closeable Example of LineIterator Class Here is the input file we need to parse − Welcome to TutorialsPoint. Simply Easy Learning. Learn web technologies, prepare exams, code online, all at one place. IOTester.java import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.commons.io.LineIterator; public class IOTester { public static void main(String[] args) { try { usingLineIterator(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingLineIterator() throws IOException { //get the file object File file = FileUtils.getFile(“input.txt”); try(LineIterator lineIterator = FileUtils.lineIterator(file)) { System.out.println(“Contents of input.txt”); while(lineIterator.hasNext()) { System.out.println(lineIterator.next()); } } } } Output It will print the following result − Contents of input.txt Welcome to TutorialsPoint. Simply Easy Learning. Learn web technologies, prepare exams, code online, all at one place. Print Page Previous Next Advertisements ”;
Category: commons Io
Apache Commons IO – SuffixFileFilter ”; Previous Next SuffixFileFilter filters the files which are based on suffix. This is used in retrieving all the files of a particular type. Class Declaration Following is the declaration for org.apache.commons.io.filefilter.SuffixFileFilter Class − public class SuffixFileFilter extends AbstractFileFilter implements Serializable Example of SuffixFileFilter Class Here is the input file we need to parse − Welcome to TutorialsPoint. Simply Easy Learning. Let”s print all files and directories in the current directory and then, filter a file with extension txt. IOTester.java import java.io.File; import java.io.IOException; import org.apache.commons.io.filefilter.SuffixFileFilter; public class IOTester { public static void main(String[] args) { try { usingSuffixFileFilter(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingSuffixFileFilter() throws IOException { //get the current directory File currentDirectory = new File(“.”); //get names of all files and directory in current directory String[] files = currentDirectory.list(); System.out.println(“All files and Folders.n”); for( int i = 0; i < files.length; i++ ) { System.out.println(files[i]); } System.out.println(“nFile with extenstion txtn”); String[] filesNames = currentDirectory.list( new SuffixFileFilter(“txt”)); for( int i = 0; i < filesNames.length; i++ ) { System.out.println(filesNames[i]); } } } Output It will print the following result. All files and Folders. .classpath .project .settings bin input.txt src File with extenstion txt input.txt Print Page Previous Next Advertisements ”;
Apache Commons IO – SizeFileComparator ”; Previous Next SizeFileComparator compare the sizes of two files/directory. It can be used to sort the lists or arrays of files using their size or directories, based on their number of children. Class Declaration Following is the declaration for org.apache.commons.io.comparator.SizeFileComparator Class − public class SizeFileComparator extends Object implements Serializable Example of SizeFileComparator Class Here is the input file we need to parse − Welcome to TutorialsPoint. Simply Easy Learning. IOTester.java import java.io.File; import java.io.FileFilter; import java.io.IOException; import org.apache.commons.io.comparator.SizeFileComparator; import org.apache.commons.io.filefilter.FileFileFilter; public class IOTester { public static void main(String[] args) { try { usingSizeFileComparator(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingSizeFileComparator() throws IOException { //get the current directory File currentDirectory = new File(“.”); SizeFileComparator comparator = new SizeFileComparator(); File[] sortedFiles = comparator.sort(currentDirectory.listFiles((FileFilter)FileFileFilter.FILE)); System.out.println(“Sorted By Size: “); for(File file:sortedFiles) { System.out.println(file.getName() + “, size(kb) :” + file.length()); } } } Output It will print the following result. Sorted By Size: input.txt, size:124 .project, size:382 .classpath, size:441 Print Page Previous Next Advertisements ”;
Apache Commons IO – Home
Apache Commons IO Tutorial PDF Version Quick Guide Resources Job Search Discussion The Apache Commons IO are the components of the Apache Commons which are derived from Java API and provides various utility classes for common operations for File IO covering wide range of use cases. It helps avoid writing boilerplate code. This tutorial covers most of the topics required for a basic understanding of Apache Commons IO and to get a feel of how it works. Audience This tutorial has been prepared for the beginners to help them understand the basic to advanced concepts related to Apache Commons IO. Prerequisites Before you start practicing various types of examples given in this reference, we assume that you are already aware about computer programs and computer programming languages. Print Page Previous Next Advertisements ”;
Apache Commons IO – Overview
Apache Commons IO – Overview ”; Previous Next Apache Commons IO library provides various utility classes for common operations for File IO covering wide range of use cases. It helps avoid writing boilerplate code. Classes Apache Commons IO library provides classes for following categories − Utility classes These classes which are under org.apache.commons.io package, provides file and string comparison. Following are some of the examples. IOUtils − Provides utility methods for reading, writing and copying files. The methods work with InputStream, OutputStream, Reader and Writer. FilenameUtils − Provides method to work with file names without using File Object. It works on different operating systems in similar way. FileUtils − Provides method to manipulates files like moving, opening, checking existence, reading of file etc. These methods use File Object. IOCase − Provides method for string manipulation and comparison. FileSystemUtils − Provides method to get the free space on a disk drive. LineIterator − Provides a flexible way to work with a line-based file. Filter classes Filter classes which are under org.apache.commons.io.filefilter package, provides methods to filter files based on logical criteria instead of string based tedious comparisons. Following are some of the examples. NameFileFilter − Filters file-names for a name. WildcardFileFilter − Filters files using the supplied wildcards. SuffixFileFilter − Filters files based on suffix. This is used in retrieving all the files of a particular type. PrefixFileFilter − Filters files based on prefix. OrFileFilter − Provides conditional OR logic across a list of file filters. Returns true, if any filters in the list return true. Otherwise, it returns false. AndFileFilter − Provides conditional and logic across a list of file filters. Returns false if any filters in the list return false. Otherwise, it returns true. File Monitor classes File monitor classes which are under org.apache.commons.io.monitor package, provides control to track changes in a specific file or folder and allows to do action accordingly on the changes. Following are some of the examples. FileEntry − Provides the state of a file or directory. File attributes at a point in time. FileAlterationObserver − Represents the state of files below a root directory, checks the file system and notifies listeners of create, change or delete events. FileAlterationMonitor − Represents a thread that spawns a monitoring thread triggering any registered FileAlterationObserver at a specified interval. Comparator classes File monitor classes under org.apache.commons.io.comparator package allow to compare and sort files and directories easily. NameFileComparator − Compare the names of two files. SizeFileComparator − Compare the size of two files. LastModifiedFileComparator − Compare the last modified dates of two files. Stream classes There are multiple implementation of InputStream under org.apache.commons.io.input package and of OutputStream under org.apache.commons.io.output package, to do useful tasks on streams. Following are some of the examples. NullOutputStream − Absorbs all data sent with any error. TeeOutputStream − Sends output to two streams. ByteArrayOutputStream − Faster version of JDK class. CountingOutputStream − Counts the number of bytes passed through the stream. ProxyOutputStream − Changes the calls to proxy stream. LockableFileWriter − A FileWriter to create lock files and allow simple cross thread file lock handling. Print Page Previous Next Advertisements ”;
Apache Commons IO – IOUtils
Apache Commons IO – IOUtils ”; Previous Next IOUtils 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. 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; public class IOTester { public 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 line by line public static void readUsingTraditionalWay() throws IOException { try(BufferedReader bufferReader = new BufferedReader( new InputStreamReader( new FileInputStream(“input.txt”) ) )) { String line; while( ( line = bufferReader.readLine() ) != null ) { System.out.println( line ); } } } //reading a file using IOUtils in one go public 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. Print Page Previous Next Advertisements ”;