{"id":32234,"date":"2014-10-29T16:00:08","date_gmt":"2014-10-29T14:00:08","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=32234"},"modified":"2014-11-10T22:57:39","modified_gmt":"2014-11-10T20:57:39","slug":"apache-commons-io-tutorial","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html","title":{"rendered":"Apache Commons IO Tutorial: A beginner&#8217;s guide"},"content":{"rendered":"<p style=\"text-align: justify;\"><a href=\"http:\/\/commons.apache.org\/proper\/commons-io\/\"><strong>Apache Commons IO<\/strong><\/a> is a Java library created and maintained by the <strong><a href=\"http:\/\/www.apache.org\/\">Apache Foundation<\/a><\/strong>.\u00a0It provides a multitude of classes that enable developers to do common tasks <em>easily and with much less boiler-plate code<\/em>, that needs to be written over and over again for every single project.The importance of libraries like that is huge, because they are <strong>mature and maintained by experienced developers<\/strong>, who have thought of every possible edge-case, or fixed the various bugs when they appeared.<\/p>\n<p style=\"text-align: justify;\">In this example, we are going to present some methods with varying functionality, depending on the package of\u00a0<a href=\"http:\/\/commons.apache.org\/proper\/commons-io\/javadocs\/api-release\/index.html?org\/apache\/commons\/io\/package-summary.html\"><code>org.apache.commons.io<\/code><\/a> that they belong to. We are not going to delve too deep inside the library, as it is enormous, but we are going to provide examples for some common usage that can definitely come in handy for every developer, beginner or not.<\/p>\n<h2 style=\"text-align: justify;\"><\/h2>\n<h2 style=\"text-align: justify;\">1. Apache Commons IO Example<\/h2>\n<p style=\"text-align: justify;\">The code for this example will be broken into several classes, and each of them will be representative of a particular area that <strong>Apache Commons IO<\/strong> covers. These areas are:<\/p>\n<ul style=\"text-align: justify;\">\n<li><strong>Utility classes<\/strong><\/li>\n<li><strong>Input<\/strong><\/li>\n<li><strong>Output<\/strong><\/li>\n<li><strong>Filters<\/strong><\/li>\n<li><strong>Comparators<\/strong><\/li>\n<li><strong>File Monitor<\/strong><\/li>\n<\/ul>\n<p style=\"text-align: justify;\">To make things even clearer, we are going to<strong> break down the output in chunks<\/strong>, one for each of the classes that we have created.\u00a0We have also created a directory inside the project folder (named <strong>ExampleFolder<\/strong>) which will contain the various files that will be used in this example to show the functionality of the various classes.<\/p>\n<p style=\"text-align: justify;\"><em>NOTE: In order to use <code>org.apache.commons.io<\/code>, you need to download the jar files (found <a href=\"http:\/\/commons.apache.org\/proper\/commons-io\/download_io.cgi\">here<\/a>) and add them to the build path of your Eclipse project, by <strong>right clicking on the project folder -&gt; Build Path -&gt; Add external archives.<\/strong><\/em><\/p>\n<p style=\"text-align: justify;\"><span style=\"text-decoration: underline;\"><em>ApacheCommonsExampleMain.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">public class ApacheCommonsExampleMain {\r\n\r\n    public static void main(String[] args) {\r\n        UtilityExample.runExample();\r\n        \r\n        FileMonitorExample.runExample();\r\n        \r\n        FiltersExample.runExample();\r\n        \r\n        InputExample.runExample();\r\n        \r\n        OutputExample.runExample();\r\n        \r\n        ComparatorExample.runExample();\r\n    }\r\n}\r\n<\/pre>\n<p style=\"text-align: justify;\">This is the main class that will be used to run the methods from the other classes of our example. You can comment certain classes in order to see the output that you want to.<\/p>\n<h3 style=\"text-align: justify;\"><\/h3>\n<h3 style=\"text-align: justify;\">1.1 Utility Classes<\/h3>\n<p style=\"text-align: justify;\">There are various Utility classes, inside the package\u00a0<a href=\"http:\/\/commons.apache.org\/proper\/commons-io\/javadocs\/api-2.4\/index.html?org\/apache\/commons\/io\/package-summary.html\"><code>org.apache.commons.io<\/code><\/a>, most of which have to do with file manipulation and String comparison. We have used some\u00a0of the most important ones here:<\/p>\n<ul style=\"text-align: justify;\">\n<li><a href=\"https:\/\/commons.apache.org\/proper\/commons-io\/javadocs\/api-1.4\/org\/apache\/commons\/io\/FilenameUtils.html\"><code>FilenameUtils<\/code><\/a>: This class has methods that work with<strong> file names<\/strong>, and the main point is to make life easier in every OS (works equally well in Unix and Windows systems).<\/li>\n<li><a href=\"https:\/\/commons.apache.org\/proper\/commons-io\/javadocs\/api-1.4\/org\/apache\/commons\/io\/FileUtils.html\"><code>FileUtils<\/code><\/a>: It provides methods for <strong>file manipulation<\/strong> (moving, opening and reading a file, checking if a file exists, etc).<\/li>\n<li><a href=\"https:\/\/commons.apache.org\/proper\/commons-io\/javadocs\/api-1.4\/org\/apache\/commons\/io\/IOCase.html\"><code>IOCase<\/code><\/a>: String <strong>manipulation and comparison<\/strong> methods.<\/li>\n<li><a href=\"https:\/\/commons.apache.org\/proper\/commons-io\/javadocs\/api-1.4\/org\/apache\/commons\/io\/FileSystemUtils.html\"><code>FileSystemUtils<\/code><\/a>: Its methods return the free space of a designated drive.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\"><span style=\"text-decoration: underline;\"><em>UtilityExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java;wrap-lines:false\">import java.io.File;\r\nimport java.io.IOException;\r\n\r\nimport org.apache.commons.io.FileSystemUtils;\r\nimport org.apache.commons.io.FileUtils;\r\nimport org.apache.commons.io.FilenameUtils;\r\nimport org.apache.commons.io.LineIterator;\r\nimport org.apache.commons.io.IOCase;\r\n\r\npublic final class UtilityExample {\r\n    \r\n    \/\/ We are using the file exampleTxt.txt in the folder ExampleFolder,\r\n    \/\/ and we need to provide the full path to the Utility classes.\r\n    private static final String EXAMPLE_TXT_PATH =\r\n            \"C:\\\\Users\\\\Lilykos\\\\workspace\\\\ApacheCommonsExample\\\\ExampleFolder\\\\exampleTxt.txt\";\r\n    \r\n    private static final String PARENT_DIR =\r\n            \"C:\\\\Users\\\\Lilykos\\\\workspace\\\\ApacheCommonsExample\";\r\n\r\n    public static void runExample() throws IOException {\r\n        System.out.println(\"Utility Classes example...\");\r\n        \r\n        \r\n        \/\/ FilenameUtils\r\n        \r\n        System.out.println(\"Full path of exampleTxt: \" +\r\n                FilenameUtils.getFullPath(EXAMPLE_TXT_PATH));\r\n        \r\n        System.out.println(\"Full name of exampleTxt: \" +\r\n                FilenameUtils.getName(EXAMPLE_TXT_PATH));\r\n        \r\n        System.out.println(\"Extension of exampleTxt: \" +\r\n                FilenameUtils.getExtension(EXAMPLE_TXT_PATH));\r\n        \r\n        System.out.println(\"Base name of exampleTxt: \" +\r\n                FilenameUtils.getBaseName(EXAMPLE_TXT_PATH));\r\n        \r\n        \r\n        \/\/ FileUtils\r\n        \r\n        \/\/ We can create a new File object using FileUtils.getFile(String)\r\n        \/\/ and then use this object to get information from the file.\r\n        File exampleFile = FileUtils.getFile(EXAMPLE_TXT_PATH);\r\n        LineIterator iter = FileUtils.lineIterator(exampleFile);\r\n        \r\n        System.out.println(\"Contents of exampleTxt...\");\r\n        while (iter.hasNext()) {\r\n            System.out.println(\"\\t\" + iter.next());\r\n        }\r\n        iter.close();\r\n        \r\n        \/\/ We can check if a file exists somewhere inside a certain directory.\r\n        File parent = FileUtils.getFile(PARENT_DIR);\r\n        System.out.println(\"Parent directory contains exampleTxt file: \" +\r\n                FileUtils.directoryContains(parent, exampleFile));\r\n        \r\n        \r\n        \/\/ IOCase\r\n        \r\n        String str1 = \"This is a new String.\";\r\n        String str2 = \"This is another new String, yes!\";\r\n        \r\n        System.out.println(\"Ends with string (case sensitive): \" +\r\n                IOCase.SENSITIVE.checkEndsWith(str1, \"string.\"));\r\n        System.out.println(\"Ends with string (case insensitive): \" +\r\n                IOCase.INSENSITIVE.checkEndsWith(str1, \"string.\"));\r\n        \r\n        System.out.println(\"String equality: \" +\r\n                IOCase.SENSITIVE.checkEquals(str1, str2));\r\n        \r\n        \r\n        \/\/ FileSystemUtils\r\n        System.out.println(\"Free disk space (in KB): \" + FileSystemUtils.freeSpaceKb(\"C:\"));\r\n        System.out.println(\"Free disk space (in MB): \" + FileSystemUtils.freeSpaceKb(\"C:\") \/ 1024);\r\n    }\r\n}\r\n<\/pre>\n<h4 style=\"text-align: justify;\">Output<\/h4>\n<pre class=\"brush:bash;wrap-lines:false\">Utility Classes example...\r\nFull path of exampleTxt: C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\\r\nFull name of exampleTxt: exampleTxt.txt\r\nExtension of exampleTxt: txt\r\nBase name of exampleTxt: exampleTxt\r\nContents of exampleTxt...\r\n\tThis is an example text file.\r\n\tWe will use it for experimenting with Apache Commons IO.\r\nParent directory contains exampleTxt file: true\r\nEnds with string (case sensitive): false\r\nEnds with string (case insensitive): true\r\nString equality: false\r\nFree disk space (in KB): 32149292\r\nFree disk space (in MB): 31395<\/pre>\n<h3 style=\"text-align: justify;\"><\/h3>\n<h3 style=\"text-align: justify;\">1.2 File Monitor<\/h3>\n<p style=\"text-align: justify;\">The\u00a0<code>org.apache.commons.io.monitor<\/code> package contains methods that can get specific information about a File, but more importantly, it can create handlers that can be used to track changes in a specific file or folder and take action depending on the changes. Let&#8217;s take a look on the code:<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-decoration: underline;\"><em>FileMonitorExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java;wrap-lines:false\">import java.io.File;\r\nimport java.io.IOException;\r\n\r\nimport org.apache.commons.io.FileDeleteStrategy;\r\nimport org.apache.commons.io.FileUtils;\r\nimport org.apache.commons.io.monitor.FileAlterationListenerAdaptor;\r\nimport org.apache.commons.io.monitor.FileAlterationMonitor;\r\nimport org.apache.commons.io.monitor.FileAlterationObserver;\r\nimport org.apache.commons.io.monitor.FileEntry;\r\n\r\n\r\npublic final class FileMonitorExample {\r\n    \r\n    private static final String EXAMPLE_PATH =\r\n            \"C:\\\\Users\\\\Lilykos\\\\workspace\\\\ApacheCommonsExample\\\\ExampleFolder\\\\exampleFileEntry.txt\";\r\n    \r\n    private static final String PARENT_DIR =\r\n            \"C:\\\\Users\\\\Lilykos\\\\workspace\\\\ApacheCommonsExample\\\\ExampleFolder\";\r\n    \r\n    private static final String NEW_DIR =\r\n            \"C:\\\\Users\\\\Lilykos\\\\workspace\\\\ApacheCommonsExample\\\\ExampleFolder\\\\newDir\";\r\n    \r\n    private static final String NEW_FILE =\r\n            \"C:\\\\Users\\\\Lilykos\\\\workspace\\\\ApacheCommonsExample\\\\ExampleFolder\\\\newFile.txt\";\r\n\r\n    public static void runExample() {\r\n        System.out.println(\"File Monitor example...\");\r\n        \r\n        \r\n        \/\/ FileEntry\r\n        \r\n        \/\/ We can monitor changes and get information about files\r\n        \/\/ using the methods of this class.\r\n        FileEntry entry = new FileEntry(FileUtils.getFile(EXAMPLE_PATH));\r\n        \r\n        System.out.println(\"File monitored: \" + entry.getFile());\r\n        System.out.println(\"File name: \" + entry.getName());\r\n        System.out.println(\"Is the file a directory?: \" + entry.isDirectory());\r\n        \r\n        \r\n        \/\/ File Monitoring\r\n        \r\n        \/\/ Create a new observer for the folder and add a listener\r\n        \/\/ that will handle the events in a specific directory and take action.\r\n        File parentDir = FileUtils.getFile(PARENT_DIR);\r\n        \r\n        FileAlterationObserver observer = new FileAlterationObserver(parentDir);\r\n        observer.addListener(new FileAlterationListenerAdaptor() {\r\n            \r\n                @Override\r\n                public void onFileCreate(File file) {\r\n                    System.out.println(\"File created: \" + file.getName());\r\n                }\r\n                \r\n                @Override\r\n                public void onFileDelete(File file) {\r\n                    System.out.println(\"File deleted: \" + file.getName());\r\n                }\r\n                \r\n                @Override\r\n                public void onDirectoryCreate(File dir) {\r\n                    System.out.println(\"Directory created: \" + dir.getName());\r\n                }\r\n                \r\n                @Override\r\n                public void onDirectoryDelete(File dir) {\r\n                    System.out.println(\"Directory deleted: \" + dir.getName());\r\n                }\r\n        });\r\n        \r\n        \/\/ Add a monior that will check for events every x ms,\r\n        \/\/ and attach all the different observers that we want.\r\n        FileAlterationMonitor monitor = new FileAlterationMonitor(500, observer);\r\n        try {\r\n            monitor.start();\r\n        \r\n            \/\/ After we attached the monitor, we can create some files and directories\r\n            \/\/ and see what happens!\r\n            File newDir = new File(NEW_DIR);\r\n            File newFile = new File(NEW_FILE);\r\n            \r\n            newDir.mkdirs();\r\n            newFile.createNewFile();\r\n                \r\n            Thread.sleep(1000);\r\n            \r\n            FileDeleteStrategy.NORMAL.delete(newDir);\r\n            FileDeleteStrategy.NORMAL.delete(newFile);\r\n            \r\n            Thread.sleep(1000);\r\n            \r\n            monitor.stop();\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        } catch (InterruptedException e) {\r\n            e.printStackTrace();\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}<\/pre>\n<h4 style=\"text-align: justify;\">Output<\/h4>\n<pre class=\"brush:java\">File Monitor example...\r\nFile monitored: C:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\exampleFileEntry.txt\r\nFile name: exampleFileEntry.txt\r\nIs the file a directory?: false\r\nDirectory created: newDir\r\nFile created: newFile.txt\r\nDirectory deleted: newDir\r\nFile deleted: newFile.txt\r\n<\/pre>\n<p style=\"text-align: justify;\">Let&#8217;s take a look on what happened here. We used some classes of the <code>org.apache.commons.io.monitor<\/code> package, that enable us to create <strong>handlers that listen to specific events<\/strong> (in our case, everything that has to do with files, folders, directories etc). In order to achieve that, there are certain steps that need to be taken:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<ol style=\"text-align: justify;\">\n<li>Create a <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/io\/File.html\"><code>File<\/code> <\/a>object, that is a reference to the directory that we want to listen to for changes.<\/li>\n<li>Create a\u00a0<a href=\"http:\/\/commons.apache.org\/proper\/commons-io\/apidocs\/org\/apache\/commons\/io\/monitor\/FileAlterationObserver.html\"><code>FileAlterationObserver<\/code><\/a> object, that will observe for those changes.<\/li>\n<li>Add a\u00a0<a href=\"http:\/\/commons.apache.org\/proper\/commons-io\/apidocs\/org\/apache\/commons\/io\/monitor\/FileAlterationListenerAdaptor.html\"><code>FileAlterationListenerAdaptor<\/code> <\/a>to the observer using the <code>addListener()<\/code> method. You can create the adaptor using various ways, but in our example we used a nested class that implements only some of the methods (the ones we need for the example requirements).<\/li>\n<li>Create a\u00a0<a href=\"http:\/\/commons.apache.org\/proper\/commons-io\/apidocs\/org\/apache\/commons\/io\/monitor\/FileAlterationMonitor.html\"><code>FileAlterationMonitor<\/code> <\/a>and add the observers that you have, as well as the interval (in ms).<\/li>\n<li>Start the monitor using the <code>start()<\/code> method and stop it when necessary using the <code>stop()<\/code> method.<\/li>\n<\/ol>\n<h3 style=\"text-align: justify;\"><\/h3>\n<h3 style=\"text-align: justify;\">1.3 Filters<\/h3>\n<p style=\"text-align: justify;\">Filters can be used in <strong>a variety of combinations and ways<\/strong>. Their job is to allow us to easily<strong> make distinctions between files<\/strong> and get the ones that satisfy certain criteria. We can also combine filters to <strong>perform logical comparisons<\/strong> and get our files much more precisely, without using tedious String comparisons\u00a0afterwards.<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-decoration: underline;\"><em>FiltersExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java;wrap-lines:false\">import java.io.File;\r\n\r\nimport org.apache.commons.io.FileUtils;\r\nimport org.apache.commons.io.IOCase;\r\nimport org.apache.commons.io.filefilter.AndFileFilter;\r\nimport org.apache.commons.io.filefilter.NameFileFilter;\r\nimport org.apache.commons.io.filefilter.NotFileFilter;\r\nimport org.apache.commons.io.filefilter.OrFileFilter;\r\nimport org.apache.commons.io.filefilter.PrefixFileFilter;\r\nimport org.apache.commons.io.filefilter.SuffixFileFilter;\r\nimport org.apache.commons.io.filefilter.WildcardFileFilter;\r\n\r\npublic final class FiltersExample {\r\n    \r\n    private static final String PARENT_DIR =\r\n            \"C:\\\\Users\\\\Lilykos\\\\workspace\\\\ApacheCommonsExample\\\\ExampleFolder\";\r\n\r\n    public static void runExample() {\r\n        System.out.println(\"File Filter example...\");\r\n        \r\n        \r\n        \/\/ NameFileFilter\r\n        \/\/ Right now, in the parent directory we have 3 files:\r\n        \/\/      directory example\r\n        \/\/      file exampleEntry.txt\r\n        \/\/      file exampleTxt.txt\r\n        \r\n        \/\/ Get all the files in the specified directory\r\n        \/\/ that are named \"example\".\r\n        File dir = FileUtils.getFile(PARENT_DIR);\r\n        String[] acceptedNames = {\"example\", \"exampleTxt.txt\"};\r\n        for (String file: dir.list(new NameFileFilter(acceptedNames, IOCase.INSENSITIVE))) {\r\n            System.out.println(\"File found, named: \" + file);\r\n        }\r\n        \r\n        \r\n        \/\/WildcardFileFilter\r\n        \/\/ We can use wildcards in order to get less specific results\r\n        \/\/      ? used for 1 missing char\r\n        \/\/      * used for multiple missing chars\r\n        for (String file: dir.list(new WildcardFileFilter(\"*ample*\"))) {\r\n            System.out.println(\"Wildcard file found, named: \" + file);\r\n        }\r\n        \r\n        \r\n        \/\/ PrefixFileFilter \r\n        \/\/ We can also use the equivalent of startsWith\r\n        \/\/ for filtering files.\r\n        for (String file: dir.list(new PrefixFileFilter(\"example\"))) {\r\n            System.out.println(\"Prefix file found, named: \" + file);\r\n        }\r\n        \r\n        \r\n        \/\/ SuffixFileFilter\r\n        \/\/ We can also use the equivalent of endsWith\r\n        \/\/ for filtering files.\r\n        for (String file: dir.list(new SuffixFileFilter(\".txt\"))) {\r\n            System.out.println(\"Suffix file found, named: \" + file);\r\n        }\r\n        \r\n        \r\n        \/\/ OrFileFilter \r\n        \/\/ We can use some filters of filters.\r\n        \/\/ in this case, we use a filter to apply a logical \r\n        \/\/ or between our filters.\r\n        for (String file: dir.list(new OrFileFilter(\r\n                new WildcardFileFilter(\"*ample*\"), new SuffixFileFilter(\".txt\")))) {\r\n            System.out.println(\"Or file found, named: \" + file);\r\n        }\r\n        \r\n        \/\/ And this can become very detailed.\r\n        \/\/ Eg, get all the files that have \"ample\" in their name\r\n        \/\/ but they are not text files (so they have no \".txt\" extension.\r\n        for (String file: dir.list(new AndFileFilter( \/\/ we will match 2 filters...\r\n                new WildcardFileFilter(\"*ample*\"), \/\/ ...the 1st is a wildcard...\r\n                new NotFileFilter(new SuffixFileFilter(\".txt\"))))) { \/\/ ...and the 2nd is NOT .txt.\r\n            System.out.println(\"And\/Not file found, named: \" + file);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<h4 style=\"text-align: justify;\">Output<\/h4>\n<pre class=\"brush:bash\">File Filter example...\r\nFile found, named: example\r\nFile found, named: exampleTxt.txt\r\nWildcard file found, named: example\r\nWildcard file found, named: exampleFileEntry.txt\r\nWildcard file found, named: exampleTxt.txt\r\nPrefix file found, named: example\r\nPrefix file found, named: exampleFileEntry.txt\r\nPrefix file found, named: exampleTxt.txt\r\nSuffix file found, named: exampleFileEntry.txt\r\nSuffix file found, named: exampleTxt.txt\r\nOr file found, named: example\r\nOr file found, named: exampleFileEntry.txt\r\nOr file found, named: exampleTxt.txt\r\nAnd\/Not file found, named: example\r\n<\/pre>\n<h3 style=\"text-align: justify;\"><\/h3>\n<h3 style=\"text-align: justify;\">1.4 Comparators<\/h3>\n<p style=\"text-align: justify;\">The\u00a0<code>org.apache.commons.io.comparator<\/code> package contains classes that allow us to easily compare and sort files and directories. We just need to provide a list of files and, depending on the class, compare them in various ways.<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-decoration: underline;\"><em>ComparatorExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java;wrap-lines:false\">import java.io.File;\r\nimport java.util.Date;\r\n\r\nimport org.apache.commons.io.FileUtils;\r\nimport org.apache.commons.io.IOCase;\r\nimport org.apache.commons.io.comparator.LastModifiedFileComparator;\r\nimport org.apache.commons.io.comparator.NameFileComparator;\r\nimport org.apache.commons.io.comparator.SizeFileComparator;\r\n\r\npublic final class ComparatorExample {\r\n    \r\n    private static final String PARENT_DIR =\r\n            \"C:\\\\Users\\\\Lilykos\\\\workspace\\\\ApacheCommonsExample\\\\ExampleFolder\";\r\n    \r\n    private static final String FILE_1 =\r\n            \"C:\\\\Users\\\\Lilykos\\\\workspace\\\\ApacheCommonsExample\\\\ExampleFolder\\\\example\";\r\n    \r\n    private static final String FILE_2 =\r\n            \"C:\\\\Users\\\\Lilykos\\\\workspace\\\\ApacheCommonsExample\\\\ExampleFolder\\\\exampleTxt.txt\";\r\n    \r\n    public static void runExample() {\r\n        System.out.println(\"Comparator example...\");\r\n        \r\n        \/\/NameFileComparator\r\n        \r\n        \/\/ Let's get a directory as a File object\r\n        \/\/ and sort all its files.\r\n        File parentDir = FileUtils.getFile(PARENT_DIR);\r\n        NameFileComparator comparator = new NameFileComparator(IOCase.SENSITIVE);\r\n        File[] sortedFiles = comparator.sort(parentDir.listFiles());\r\n        \r\n        System.out.println(\"Sorted by name files in parent directory: \");\r\n        for (File file: sortedFiles) {\r\n            System.out.println(\"\\t\"+ file.getAbsolutePath());\r\n        }\r\n        \r\n        \r\n        \/\/ SizeFileComparator\r\n        \r\n        \/\/ We can compare files based on their size.\r\n        \/\/ The boolean in the constructor is about the directories.\r\n        \/\/      true: directory's contents count to the size.\r\n        \/\/      false: directory is considered zero size.\r\n        SizeFileComparator sizeComparator = new SizeFileComparator(true);\r\n        File[] sizeFiles = sizeComparator.sort(parentDir.listFiles());\r\n        \r\n        System.out.println(\"Sorted by size files in parent directory: \");\r\n        for (File file: sizeFiles) {\r\n            System.out.println(\"\\t\"+ file.getName() + \" with size (kb): \" + file.length());\r\n        }\r\n        \r\n        \r\n        \/\/ LastModifiedFileComparator\r\n        \r\n        \/\/ We can use this class to find which file was more recently modified.\r\n        LastModifiedFileComparator lastModified = new LastModifiedFileComparator();\r\n        File[] lastModifiedFiles = lastModified.sort(parentDir.listFiles());\r\n        \r\n        System.out.println(\"Sorted by last modified files in parent directory: \");\r\n        for (File file: lastModifiedFiles) {\r\n            Date modified = new Date(file.lastModified());\r\n            System.out.println(\"\\t\"+ file.getName() + \" last modified on: \" + modified);\r\n        }\r\n        \r\n        \/\/ Or, we can also compare 2 specific files and find which one was last modified.\r\n        \/\/      returns &gt; 0 if the first file was last modified.\r\n        \/\/      returns  0)\r\n            System.out.println(\"File \" + file1.getName() + \" was modified last because...\");\r\n        else\r\n            System.out.println(\"File \" + file2.getName() + \"was modified last because...\");\r\n        \r\n        System.out.println(\"\\t\"+ file1.getName() + \" last modified on: \" +\r\n                new Date(file1.lastModified()));\r\n        System.out.println(\"\\t\"+ file2.getName() + \" last modified on: \" +\r\n                new Date(file2.lastModified()));\r\n    }\r\n}\r\n<\/pre>\n<h4 style=\"text-align: justify;\">Output<\/h4>\n<pre class=\"brush:bash;wrap-lines:false\">Comparator example...\r\nSorted by name files in parent directory: \r\n\tC:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\comparator1.txt\r\n\tC:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\comperator2.txt\r\n\tC:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\example\r\n\tC:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\exampleFileEntry.txt\r\n\tC:\\Users\\Lilykos\\workspace\\ApacheCommonsExample\\ExampleFolder\\exampleTxt.txt\r\nSorted by size files in parent directory: \r\n\texample with size (kb): 0\r\n\texampleTxt.txt with size (kb): 87\r\n\texampleFileEntry.txt with size (kb): 503\r\n\tcomperator2.txt with size (kb): 1458\r\n\tcomparator1.txt with size (kb): 4436\r\nSorted by last modified files in parent directory: \r\n\texampleTxt.txt last modified on: Sun Oct 26 14:02:22 EET 2014\r\n\texample last modified on: Sun Oct 26 23:42:55 EET 2014\r\n\tcomparator1.txt last modified on: Tue Oct 28 14:48:28 EET 2014\r\n\tcomperator2.txt last modified on: Tue Oct 28 14:48:52 EET 2014\r\n\texampleFileEntry.txt last modified on: Tue Oct 28 14:53:50 EET 2014\r\nFile example was modified last because...\r\n\texample last modified on: Sun Oct 26 23:42:55 EET 2014\r\n\texampleTxt.txt last modified on: Sun Oct 26 14:02:22 EET 2014\r\n<\/pre>\n<p style=\"text-align: justify;\">Let&#8217;s see what classes were used here:<\/p>\n<ul style=\"text-align: justify;\">\n<li><a href=\"https:\/\/commons.apache.org\/proper\/commons-io\/javadocs\/api-2.4\/org\/apache\/commons\/io\/comparator\/NameFileComparator.html\"><code>NameFileComparator<\/code><\/a>: Compares files according to their name.<\/li>\n<li><a href=\"http:\/\/commons.apache.org\/proper\/commons-io\/apidocs\/org\/apache\/commons\/io\/comparator\/SizeFileComparator.html\"><code>SizeFileComparator<\/code><\/a>: Compares files according to their size.<\/li>\n<li><a href=\"http:\/\/commons.apache.org\/proper\/commons-io\/apidocs\/org\/apache\/commons\/io\/comparator\/LastModifiedFileComparator.html\"><code>LastModifiedFileComparator<\/code><\/a>: Compares files according to the date they were last modified.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">You should also take notice here, that the comparisons can happen either in whole directories (were they are sorted using the <code>sort()<\/code> method), or separately for 2 files specifically (using <code>compare()<\/code>).<\/p>\n<h3 style=\"text-align: justify;\"><\/h3>\n<h3 style=\"text-align: justify;\">1.5 Input<\/h3>\n<p style=\"text-align: justify;\">There \u00a0are various implementations of <code>InputStream<\/code> in the\u00a0<code>org.apache.commons.io.input<\/code> package. We are going to examine one of the most useful, <a href=\"http:\/\/commons.apache.org\/proper\/commons-io\/apidocs\/org\/apache\/commons\/io\/input\/TeeInputStream.html\"><code>TeeInputStream<\/code><\/a>, which takes as arguments both an <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/io\/InputStream.html\"><code>InputStream<\/code> <\/a>and an <a href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/io\/OutputStream.html\"><code>OutputStream<\/code><\/a>, and automatically copies the read bytes from the input, to the output. Moreover, by using a third, boolean argument, by closing just the <code>TeeInputStream<\/code> in the end, the two additional streams close as well.<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-decoration: underline;\"><em>InputExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java;wrap-lines:false\">import java.io.ByteArrayInputStream;\r\nimport java.io.ByteArrayOutputStream;\r\nimport java.io.File;\r\nimport java.io.IOException;\r\n\r\nimport org.apache.commons.io.FileUtils;\r\nimport org.apache.commons.io.input.TeeInputStream;\r\nimport org.apache.commons.io.input.XmlStreamReader;\r\n\r\n\r\npublic final class InputExample {\r\n    \r\n    private static final String XML_PATH =\r\n            \"C:\\\\Users\\\\Lilykos\\\\workspace\\\\ApacheCommonsExample\\\\InputOutputExampleFolder\\\\web.xml\";\r\n    \r\n    private static final String INPUT = \"This should go to the output.\";\r\n\r\n    public static void runExample() {\r\n        System.out.println(\"Input example...\");\r\n        XmlStreamReader xmlReader = null;\r\n        TeeInputStream tee = null;\r\n        \r\n        try {\r\n            \r\n            \/\/ XmlStreamReader\r\n            \r\n            \/\/ We can read an xml file and get its encoding.\r\n            File xml = FileUtils.getFile(XML_PATH);\r\n            \r\n            xmlReader = new XmlStreamReader(xml);\r\n            System.out.println(\"XML encoding: \" + xmlReader.getEncoding());\r\n            \r\n            \r\n            \/\/ TeeInputStream\r\n            \r\n            \/\/ This very useful class copies an input stream to an output stream\r\n            \/\/ and closes both using only one close() method (by defining the 3rd\r\n            \/\/ constructor parameter as true).\r\n            ByteArrayInputStream in = new ByteArrayInputStream(INPUT.getBytes(\"US-ASCII\"));\r\n            ByteArrayOutputStream out = new ByteArrayOutputStream();\r\n            \r\n            tee = new TeeInputStream(in, out, true);\r\n            tee.read(new byte[INPUT.length()]);\r\n\r\n            System.out.println(\"Output stream: \" + out.toString());         \r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            try { xmlReader.close(); }\r\n            catch (IOException e) { e.printStackTrace(); }\r\n            \r\n            try { tee.close(); }\r\n            catch (IOException e) { e.printStackTrace(); }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<h4 style=\"text-align: justify;\">Output<\/h4>\n<pre class=\"brush:bash\">Input example...\r\nXML encoding: UTF-8\r\nOutput stream: This should go to the output.\r\n<\/pre>\n<h3 style=\"text-align: justify;\"><\/h3>\n<h3 style=\"text-align: justify;\">1.6 Output<\/h3>\n<p style=\"text-align: justify;\">Similar to the\u00a0<code>org.apache.commons.io.input<\/code>,\u00a0<code>org.apache.commons.io.output<\/code> has implementations of <code>OutputStream<\/code>, that can be used in many situations. A very interesting one is <a href=\"http:\/\/commons.apache.org\/proper\/commons-io\/apidocs\/org\/apache\/commons\/io\/output\/TeeOutputStream.html\"><code>TeeOutputStream<\/code><\/a>, which allows an output stream to be branched, or in other words, we can send an input stream to 2 different outputs.<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-decoration: underline;\"><em>OutputExample.java<\/em><\/span><\/p>\n<pre class=\"brush:java;wrap-lines:false\">import java.io.ByteArrayInputStream;\r\nimport java.io.ByteArrayOutputStream;\r\nimport java.io.IOException;\r\n\r\nimport org.apache.commons.io.input.TeeInputStream;\r\nimport org.apache.commons.io.output.TeeOutputStream;\r\n\r\npublic final class OutputExample {\r\n    \r\n    private static final String INPUT = \"This should go to the output.\";\r\n\r\n    public static void runExample() {\r\n        System.out.println(\"Output example...\");\r\n        TeeInputStream teeIn = null;\r\n        TeeOutputStream teeOut = null;\r\n        \r\n        try {\r\n            \r\n            \/\/ TeeOutputStream\r\n            \r\n            ByteArrayInputStream in = new ByteArrayInputStream(INPUT.getBytes(\"US-ASCII\"));\r\n            ByteArrayOutputStream out1 = new ByteArrayOutputStream();\r\n            ByteArrayOutputStream out2 = new ByteArrayOutputStream();\r\n            \r\n            teeOut = new TeeOutputStream(out1, out2);\r\n            teeIn = new TeeInputStream(in, teeOut, true);\r\n            teeIn.read(new byte[INPUT.length()]);\r\n\r\n            System.out.println(\"Output stream 1: \" + out1.toString());\r\n            System.out.println(\"Output stream 2: \" + out2.toString());\r\n            \r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            \/\/ No need to close teeOut. When teeIn closes, it will also close its\r\n            \/\/ Output stream (which is teeOut), which will in turn close the 2\r\n            \/\/ branches (out1, out2).\r\n            try { teeIn.close(); }\r\n            catch (IOException e) { e.printStackTrace(); }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<h4 style=\"text-align: justify;\">Output<\/h4>\n<pre class=\"brush:bash\">Output example...\r\nOutput stream 1: This should go to the output.\r\nOutput stream 2: This should go to the output.\r\n<\/pre>\n<h2 style=\"text-align: justify;\"><\/h2>\n<h2 style=\"text-align: justify;\">2. Download the Complete Example<\/h2>\n<p style=\"text-align: justify;\">This was an introduction to <strong>Apache Commons IO<\/strong>, covering most of the important classes that provide easy solutions to developers. There are many other capabilities in this vast package,but using this intro you get the general idea and a handful of useful tools for your future projects!<\/p>\n<div class=\"download\"><strong>Download<br \/><\/strong>You can download the full source code of this example here:  <strong><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/ApacheCommonsIOExample.rar\">ApacheCommonsIOExample.rar<\/a><\/strong><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apache Commons IO is a Java library created and maintained by the Apache Foundation.\u00a0It provides a multitude of classes that enable developers to do common tasks easily and with much less boiler-plate code, that needs to be written over and over again for every single project.The importance of libraries like that is huge, because they &hellip;<\/p>\n","protected":false},"author":600,"featured_media":32236,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[368],"class_list":["post-32234","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-apache-commons"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Apache Commons IO Tutorial: A beginner&#039;s guide<\/title>\n<meta name=\"description\" content=\"We have prepared a comprehensive example to get you started with the great Apache Commons IO library. Examples on Utility classes, File Monitor, Filters, Comparators, Input, Output, and more!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apache Commons IO Tutorial: A beginner&#039;s guide\" \/>\n<meta property=\"og:description\" content=\"We have prepared a comprehensive example to get you started with the great Apache Commons IO library. Examples on Utility classes, File Monitor, Filters, Comparators, Input, Output, and more!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2014-10-29T14:00:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-11-10T20:57:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/apache-commons-io-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ilias Koutsakis\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ilias Koutsakis\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html\"},\"author\":{\"name\":\"Ilias Koutsakis\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/214a958f3242fd096556ce923ac6dc01\"},\"headline\":\"Apache Commons IO Tutorial: A beginner&#8217;s guide\",\"datePublished\":\"2014-10-29T14:00:08+00:00\",\"dateModified\":\"2014-11-10T20:57:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html\"},\"wordCount\":950,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2014\\\/10\\\/apache-commons-io-logo.jpg\",\"keywords\":[\"Apache Commons\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html\",\"name\":\"Apache Commons IO Tutorial: A beginner's guide\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2014\\\/10\\\/apache-commons-io-logo.jpg\",\"datePublished\":\"2014-10-29T14:00:08+00:00\",\"dateModified\":\"2014-11-10T20:57:39+00:00\",\"description\":\"We have prepared a comprehensive example to get you started with the great Apache Commons IO library. Examples on Utility classes, File Monitor, Filters, Comparators, Input, Output, and more!\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2014\\\/10\\\/apache-commons-io-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2014\\\/10\\\/apache-commons-io-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2014\\\/10\\\/apache-commons-io-tutorial.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Apache Commons IO Tutorial: A beginner&#8217;s guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/214a958f3242fd096556ce923ac6dc01\",\"name\":\"Ilias Koutsakis\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/99a1bed6ec4b4df2b97cdc90cb2915a4d01289c10980c50e542d9602dec59b5c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/99a1bed6ec4b4df2b97cdc90cb2915a4d01289c10980c50e542d9602dec59b5c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/99a1bed6ec4b4df2b97cdc90cb2915a4d01289c10980c50e542d9602dec59b5c?s=96&d=mm&r=g\",\"caption\":\"Ilias Koutsakis\"},\"description\":\"Ilias has graduated from the Department of Informatics and Telecommunications of the National and Kapodistrian University of Athens. He is interested in all aspects of software engineering, particularly data mining, and loves the challenge of working with new technologies. He is pursuing the dream of clean and readable code on a daily basis.\",\"sameAs\":[\"http:\\\/\\\/www.javacodegeeks.com\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/ilias-koutsakis\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apache Commons IO Tutorial: A beginner's guide","description":"We have prepared a comprehensive example to get you started with the great Apache Commons IO library. Examples on Utility classes, File Monitor, Filters, Comparators, Input, Output, and more!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html","og_locale":"en_US","og_type":"article","og_title":"Apache Commons IO Tutorial: A beginner's guide","og_description":"We have prepared a comprehensive example to get you started with the great Apache Commons IO library. Examples on Utility classes, File Monitor, Filters, Comparators, Input, Output, and more!","og_url":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2014-10-29T14:00:08+00:00","article_modified_time":"2014-11-10T20:57:39+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/apache-commons-io-logo.jpg","type":"image\/jpeg"}],"author":"Ilias Koutsakis","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Ilias Koutsakis","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html"},"author":{"name":"Ilias Koutsakis","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/214a958f3242fd096556ce923ac6dc01"},"headline":"Apache Commons IO Tutorial: A beginner&#8217;s guide","datePublished":"2014-10-29T14:00:08+00:00","dateModified":"2014-11-10T20:57:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html"},"wordCount":950,"commentCount":2,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/apache-commons-io-logo.jpg","keywords":["Apache Commons"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html","url":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html","name":"Apache Commons IO Tutorial: A beginner's guide","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/apache-commons-io-logo.jpg","datePublished":"2014-10-29T14:00:08+00:00","dateModified":"2014-11-10T20:57:39+00:00","description":"We have prepared a comprehensive example to get you started with the great Apache Commons IO library. Examples on Utility classes, File Monitor, Filters, Comparators, Input, Output, and more!","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/apache-commons-io-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2014\/10\/apache-commons-io-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2014\/10\/apache-commons-io-tutorial.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Apache Commons IO Tutorial: A beginner&#8217;s guide"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/214a958f3242fd096556ce923ac6dc01","name":"Ilias Koutsakis","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/99a1bed6ec4b4df2b97cdc90cb2915a4d01289c10980c50e542d9602dec59b5c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/99a1bed6ec4b4df2b97cdc90cb2915a4d01289c10980c50e542d9602dec59b5c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/99a1bed6ec4b4df2b97cdc90cb2915a4d01289c10980c50e542d9602dec59b5c?s=96&d=mm&r=g","caption":"Ilias Koutsakis"},"description":"Ilias has graduated from the Department of Informatics and Telecommunications of the National and Kapodistrian University of Athens. He is interested in all aspects of software engineering, particularly data mining, and loves the challenge of working with new technologies. He is pursuing the dream of clean and readable code on a daily basis.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/ilias-koutsakis"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/32234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=32234"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/32234\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/32236"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=32234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=32234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=32234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}