{"id":67,"date":"2021-01-16T21:11:23","date_gmt":"2021-01-16T21:11:23","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/?page_id=67"},"modified":"2021-01-16T21:29:55","modified_gmt":"2021-01-16T21:29:55","slug":"i-o-files-streams","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/","title":{"rendered":"I\/O, Files &#038; Streams"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a6d4a88297af\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a6d4a88297af\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/#Files_Directories\" >Files &amp; Directories<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/#The_File_Class\" >The File Class<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/#Using_Class_File\" >Using Class&nbsp;File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/#Combining_File_Path_and_Name\" >Combining File Path and Name<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/#What_File_Is_Not\" >What&nbsp;File&nbsp;Is Not<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/#Listing_Files_Directories\" >Listing Files &amp; Directories<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/course-topics\/i-o-files-streams\/#Listing_Directory_Contents_with_a_Filter\" >Listing Directory Contents with a Filter<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">Now, let&#8217;s talk about Input\/Output, Files, and Streams.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Files_Directories\"><\/span>Files &amp; Directories<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you wanted to learn how to read and write files from Java, you might be tempted to look first at the class <strong>File<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It turns out that you do not use class <strong>File<\/strong> to read and write files, but it is a useful class that represents a file or a directory.&nbsp;&nbsp; Here are some details about what it can do&#8230;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_File_Class\"><\/span>The File Class<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<strong>File<\/strong>&nbsp;class encapsulates the attributes and behavior needed to represent a file or a directory on the native system. The <strong>File<\/strong> class<em><strong>&nbsp;is not<\/strong><\/em>&nbsp;concerned with the contents of the file; it&nbsp;<em><strong>is<\/strong><\/em>&nbsp;concerned with the storage of the file on disk.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use an instance of the&nbsp;<strong>File<\/strong>&nbsp;class to perform certain operations on a corresponding file on the native system:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>delete()<\/code><\/li><li><code>renameTo()<\/code><\/li><li><code>mkdir()<\/code>&nbsp;and&nbsp;<code>mkdirs()<\/code><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Note that, while you can create a directory using the&nbsp;<code>File<\/code>&nbsp;class (<code><strong>mkdir()<\/strong><\/code>), you do&nbsp;<em><strong>not<\/strong><\/em>&nbsp;create a file through the&nbsp;<code><strong>File<\/strong><\/code>&nbsp;class. Creating a file is an operation performed by the stream classes, which we&#8217;ll discuss later.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_Class_File\"><\/span>Using Class&nbsp;<code>File<\/code><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of using&nbsp;<code><strong>File<\/strong><\/code>&nbsp;to rename a file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage inputOutput;\n\nimport java.io.File;\n\npublic class RenameFile\n{\n    public static void main(String&#x5B;] args)\n    {\n        File fromFile = new File(&quot;myFile.txt&quot;);\n        File toFile   = new File(&quot;yourFile.txt&quot;);\n        boolean renamed = fromFile.renameTo(toFile);\n        if (renamed)\n            System.out.println(&quot;File was successfully renamed&quot;);\n        else\n            System.out.println(&quot;File was not renamed&quot;);\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Note that no exception is thrown when the rename fails; you have to check the return value. The&nbsp;<code><strong>delete()<\/strong><\/code>&nbsp;method has the same return value convention.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Combining_File_Path_and_Name\"><\/span>Combining File Path and Name<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If you wish to write portable Java code, do&nbsp;<em><strong><u>not<\/u><\/strong><\/em>&nbsp;do the following:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage inputOutput;\n\nimport java.io.File;\n\n\/**\n *  This class creates an instance of File in a non-portable way.\n *\/\npublic class FilePathName\n{\n    public static void main(String&#x5B;] args)\n    {\n        File file = new File(&quot;path\/path1\/name.txt&quot;);\n        \n        \/\/ ...\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The above is UNIX-specific (although it will also work on Microsoft Windows).&nbsp;<em><strong>It can&#8217;t be assumed to work on other platforms<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Class&nbsp;<code><strong>File<\/strong><\/code>&nbsp;has the following, which you might think would help:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code><strong>public static final String separator<\/strong><\/code>&nbsp;&#8212; set to the value of the&nbsp;<code><strong>System<\/strong><\/code>&nbsp;property&nbsp;<code><strong>file.separator<\/strong><\/code><\/li><li><code><strong>public static final char separatorChar<\/strong><\/code>&nbsp;&#8212; set to the value of the&nbsp;<code><strong>System<\/strong><\/code>&nbsp;property&nbsp;<code><strong>file.separator<\/strong><\/code><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">(Note that&nbsp;<code><strong>File<\/strong><\/code>&nbsp;also has the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code><strong>public static final String pathSeparator<\/strong><\/code>&nbsp;&#8212; set to the value of the&nbsp;<code><strong>System<\/strong><\/code>&nbsp;property&nbsp;<code><strong>path.separator<\/strong><\/code><\/li><li><strong><code>public static final char pathSeparatorChar<\/code>&nbsp;<\/strong>&#8212; set to the value of the&nbsp;<code><strong>System<\/strong><\/code>&nbsp;property&nbsp;<code><strong>path.separator<\/strong><\/code><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">but these relate to&nbsp;<code><strong>CLASSPATH<\/strong><\/code>&nbsp;separator characters, not file path separator characters.)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you might think that something like:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage inputOutput;\n\nimport java.io.File;\n\n\/**\n *  This class creates an instance of File in a non-portable way.\n *\/\npublic class FilePathName\n{\n    public static void main(String&#x5B;] args)\n    {\n        File file = new File(&quot;path&quot; + File.separator +\n                             &quot;path1&quot; + File.separator +\n                             &quot;name.txt&quot;);\n        \n        \/\/ ...\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">would make things portable,&nbsp;<em><strong>but it doesn&#8217;t<\/strong><\/em>. There are some computer systems which do not use the pure file separator convention to separate directories from the files they contain.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">How can we make things more portable? Well, take a look at the constructors available in the&nbsp;<code>File<\/code>&nbsp;class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>public File(String path)<\/code><\/li><li><code>public File(String path, String name)<\/code><\/li><li><code>public File(File dir, String name)<\/code><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s see how we can use these constructors to solve the portability problem (at least to the extent that we can):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage inputOutput;\n\nimport java.io.File;\n\n\/**\n *  This class creates an instance of File in a portable way.\n *\/\npublic class FilePathName\n{\n    public static void main(String&#x5B;] args)\n    {\n        File path = new File(&quot;path&quot;);\n        path = new File(path, &quot;path1&quot;);\n        path = new File(path, &quot;name.txt&quot;);\n        System.out.println(&quot;File path = &#039;&quot; + \n                           path.getAbsolutePath() + &quot;&#039;&quot;);\n        \n        \/\/ ...\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Notice how no explicit separators are used at all. If you type this in (or copy it from this page and paste it into an editor), you&#8217;ll find that the&nbsp;<code>println<\/code>&nbsp;produces a full (absolute) path specification (despite the fact that there is probably no such file on your system). Note that we entered a&nbsp;<em>relative<\/em>&nbsp;path specification.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_File_Is_Not\"><\/span>What&nbsp;<code>File<\/code>&nbsp;Is Not<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">It is important to note that an instance of class&nbsp;<code><strong>File<\/strong><\/code>&nbsp;merely&nbsp;<em>represents<\/em>&nbsp;a file or a directory. Creating an instance of class&nbsp;<code><strong>File<\/strong><\/code>&nbsp;does&nbsp;<em><strong>not<\/strong><\/em>&nbsp;create a corresponding file on the native system. Further, an instance of class&nbsp;<code><strong>File<\/strong><\/code>&nbsp;may exist without there necessarily existing a corresponding file. However, using an instance of class&nbsp;<code><strong>File<\/strong><\/code>, you can discover a number of things about the corresponding file, including whether it exists or not:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>canRead()<\/code>&nbsp;and&nbsp;<code>canWrite()<\/code><\/li><li><code>exists()<\/code><\/li><li><code>getName()<\/code>&nbsp;and&nbsp;<code>getPath()<\/code><\/li><li><code>isAbsolute()<\/code><\/li><li><code>getAbsolutePath()<\/code>&nbsp;and&nbsp;<code>getCanonicalPath()<\/code><\/li><li><code>isFile()<\/code>&nbsp;and&nbsp;<code>isDirectory()<\/code><\/li><li><code>lastModified()<\/code><\/li><li><code>length()<\/code><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Listing_Files_Directories\"><\/span>Listing Files &amp; Directories<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When an instance of&nbsp;<code><strong>File<\/strong><\/code>&nbsp;represents a directory, the&nbsp;<code><strong>list()<\/strong><\/code>&nbsp;methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>list()<\/code><\/li><li><code>list(FilenameFilter filter)<\/code><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">may be used to obtain lists of files in that directory. Here&#8217;s an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage inputOutput;\n\nimport java.io.File;\nimport java.io.IOException;\nimport java.util.ArrayList;\nimport java.util.Iterator;\nimport java.util.Properties;\n\n\/**\n *  This class finds the current directory, gets a list of all\n *  the files\/directories in the current directory, separates\n *  the files from the directories, and prints out the two lists.\n *\/\npublic class DirectoryLister\n{\n    public static void main(String&#x5B;] args)\n    {\n        \/\/ Get current directory\n        File dir = new File( System.getProperty(&quot;user.dir&quot;) );\n        \/\/ Print out its contents\n        printDirectoryContents(dir);\n    }\n    \n    public static void printDirectoryContents(File dir)\n    {\n        if (!dir.isDirectory())\n            return;\n            \n        try\n        {\n            System.out.println(&quot;Directory : &#039;&quot; + \n                                dir.getCanonicalPath() + &quot;&#039;&quot;);\n            \/\/ Get the list of directory contents\n            String&#x5B;] contents = dir.list();\n            \/\/ Go through the contents, separating out the files vs dirs.\n            ArrayList&lt;String&gt; dirs  = new ArrayList&lt;String&gt;();\n            ArrayList&lt;String&gt; files = new ArrayList&lt;String&gt;();\n            for (int i = 0; i &lt; contents.length; i++)\n            {\n                File file = new File(contents&#x5B;i]);\n                if (file.isFile())\n                    files.add(contents&#x5B;i]); \/\/ Add file name\n                else if (file.isDirectory())\n                    dirs.add(contents&#x5B;i]); \/\/ Add dir name\n            }\n            \/\/ Print out the list of directories\n            System.out.println(&quot;Directories:&quot;);\n            Iterator&lt;String&gt; it = null;\n            for (it = dirs.iterator(); it.hasNext(); )\n            {\n                String name = it.next();\n                System.out.println(&quot; &quot; + name);\n            }\n            \/\/ Print out the list of files\n            System.out.println(&quot;Files:&quot;);\n            for (it = files.iterator(); it.hasNext(); )\n            {\n                String name = it.next();\n                System.out.println(&quot; &quot; + name);\n            }\n        }\n        catch (IOException e)\n        {\n            e.printStackTrace();\n        }\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">When I ran this program directly from within my NetBeans IDE, it gave me the following output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Directory : '...\\NetBeansProjects\\InputOutput'\nDirectories:\n build\n dist\n nbproject\n src\n test\nFiles:\n .cvsignore\n build.xml\n manifest.mf<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As expected, the above program only lists the directories and files in the top level directory.&nbsp; That is, it does not recurse down into the directory hierarchy.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Listing_Directory_Contents_with_a_Filter\"><\/span>Listing Directory Contents with a Filter<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code><strong>list(FilenameFilter filter)<\/strong><\/code>&nbsp;form may be used, in combination with an appropriate&nbsp;<code><strong>FilenameFilter<\/strong><\/code>, to produce a more selective list. For example, the list could be made to include only those files:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>whose names begin with &#8220;foo&#8221;, or end in &#8220;bar&#8221;<\/li><li>whose sizes exceed or are smaller than a certain threshold<\/li><li>which can be written to<\/li><li>any combination of the above<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><code><strong>FilenameFilter<\/strong><\/code>&nbsp;is an interface.&nbsp; Its full definition is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npublic interface FilenameFilter \n{\n    \/**\n     * Tests if a specified file should be included in a file list.\n     *\n     * @param dir the directory in which the file was found.\n     * @param name the name of the file.\n     * @return &lt;code&gt;true&lt;\/code&gt; if the name should be included in the file\n     * list; &lt;code&gt;false&lt;\/code&gt; otherwise.\n     * @since JDK1.0\n     *\/\n     boolean accept(File dir, String name);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Pretty simple, huh?<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><u>Note:<\/u><\/strong> There is a somewhat similar abstract class, <strong>javax.swing.filechooser.FileFilter<\/strong>, that relates (as you might expect, given the package) to Swing <strong>JFileChooser<\/strong> dialogs. It might be easy to confuse the\u00a0<strong>FileFilter<\/strong>\u00a0class with the\u00a0<strong>FilenameFilter<\/strong>\u00a0interface.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s a modification of the previous example to provide filtering of the output so that only certain files are included.&nbsp; In addition to filtering based on one or more file types, I&#8217;ve also added support for recursive directory searching (including the appropriate indentation so that the output represents the depth of recursion).&nbsp; I&#8217;ve also removed the need to use any <strong>ArrayList<\/strong>s.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage inputOutput;\n\nimport java.io.File;\nimport java.io.FilenameFilter;\nimport java.io.IOException;\nimport java.util.Properties;\n\n\/**\n *  This class finds the current directory, gets a list of all\n *  the files\/directories in the current directory, separates\n *  the files from the directories, and prints out the two lists.\n *\/\npublic class FilteredDirectoryLister\n{\n    public static void main(String&#x5B;] args)\n    {\n        \/\/ Get current directory\n        File dir = new File( System.getProperty(&quot;user.dir&quot;) );\n        \/\/ Print out its contents, using a filter\n        FileTypeFilter filter = new FileTypeFilter( \n                                      new String&#x5B;] {&quot;.xml&quot;, &quot;.properties&quot;} );   \n        printDirectoryContents(dir, filter, &quot;&quot;);\n    }\n    \n    public static void printDirectoryContents(File dir, \n                                              FileTypeFilter filter, \n                                              String indent)\n    {\n        if (!dir.isDirectory())\n            return;\n            \n        try\n        {\n            System.out.println(indent + &quot;Directory : &#039;&quot; + \n                                dir.getCanonicalPath() + &quot;&#039;&quot;);\n            \n            \/\/ Get the list of directories, using a directory filter\n            String&#x5B;] subdirs = dir.list(m_dirFilter);\n            \/\/ Recurse through the subdirectories.\n            for (String name : subdirs)\n            {\n              File subdir = new File(name);\n              printDirectoryContents(subdir, filter, indent + &quot; &quot;);\n            }\n            \n            \/\/ Now, get the list of matching files, using the file type filter\n            String&#x5B;] files = dir.list(filter);\n            \/\/ Print out the list of files\n            for (String name : files)\n            {\n                System.out.println(indent + &quot; &quot; + name);\n            }\n        }\n        catch (IOException e)\n        {\n            e.printStackTrace();\n        }\n    }\n    \n    \/\/\/\/ Private data \/\/\/\/\n    private static DirectoryFilter m_dirFilter = new DirectoryFilter();\n}\n\nclass FileTypeFilter implements FilenameFilter\n{\n    FileTypeFilter(String&#x5B;] types)\n    {\n        m_types = types;\n    }\n    \n    \/\/ Accepts only those files that have one of  \n    \/\/ the supplied set of file types.\n    public boolean accept(File dir, String name)\n    {\n        boolean includeFile = false;\n        File path = new File(dir, name);\n        if (path.isFile())\n        {\n          for (String type : m_types)\n          {\n            if (name.endsWith(type))\n            {\n              includeFile = true;\n              break;\n            }\n          }\n        }\n        return includeFile;\n    }\n    \n    \/\/\/\/ Private data \/\/\/\/\n    private String&#x5B;] m_types;\n}\n\nclass DirectoryFilter implements FilenameFilter\n{\n    \/\/ Accepts any directory\n    public boolean accept(File dir, String name)\n    {\n        boolean includeFile = false;\n        File path = new File(dir, name);\n        if (path.isDirectory())\n          includeFile = true;\n        return includeFile;\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">When I ran this from within my NetBeans IDE, it gave me the following output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Directory : '...\\NetBeansProjects\\InputOutput'\n Directory : '...\\NetBeansProjects\\InputOutput\\build'\n Directory : '...\\NetBeansProjects\\InputOutput\\dist'\n Directory : '...\\NetBeansProjects\\InputOutput\\nbproject'\n  build-impl.xml\n  genfiles.properties\n  project.properties\n  project.xml\n Directory : '...\\NetBeansProjects\\InputOutput\\src'\n Directory : '...\\NetBeansProjects\\InputOutput\\test'\n build.xml<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we do see files and directories in the entire hierarchy, and only those files that match the specified filetypes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now, let&#8217;s talk about Input\/Output, Files, and Streams. Files &amp; Directories If you wanted to learn how to read and write files from Java, you might be tempted to look first at the class File. It turns out that you do not use class File to read and write files, but it is a useful [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":19,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","ocean_front_end_style_editor":"no","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-67","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/pages\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/comments?post=67"}],"version-history":[{"count":6,"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/pages\/67\/revisions"}],"predecessor-version":[{"id":78,"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/pages\/67\/revisions\/78"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/pages\/19"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/HighOctaneJava\/wp-json\/wp\/v2\/media?parent=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}