---
title: "Java I/O: Accessing the Host File System"  
description: "Here we discuss another important class in the I/O package. The java.io.File class provides an abstract representation of a file or directory on the h"  
author: "zack mathews"  
published: 2016-05-25  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11125/java-i-o-accessing-the-host-file-system  
category: "java"  
tags: ["java"]  
reading_time: 6 minutes  

---

# Java I/O: Accessing the Host File System

Here we discuss another important class in the I/O package. The **java.io.File** class provides an abstract representation of a file or directory on the host [file system](https://www.mindstick.com/forum/157850/what-is-a-file-system-explain-it-s-used-in-system-software).\

[As mentioned](https://answers.mindstick.com/qa/38307/bring-out-the-significance-of-the-terms-sovereign-democratic-republic-as-mentioned-in-the-preamble) previously, **Java SE 7** introduced a new class called Path in the java.nio.file package. This class provides more sophisticated [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) than the File class. This does not, however, reduce the importance of the File class, and it is recommended that we study this class in the javadocs. We will use the newly introduced Path class for the program in this post. Our program will accept a path on the [command line](https://www.mindstick.com/forum/158085/how-do-i-pass-command-line-arguments-to-a-node-js-program) and list out all the files found in the specified path.

##### The Directory Listing Program

The Directory Listing program accepts an argument on the command line. The program prints the names of all the files under the path specified in this argument to the system console.

##### Program Code

The [full program](https://www.mindstick.com/forum/159166/how-do-unit-tests-go-into-the-full-program) is given here:

```
import java.nio.file.*;import java.io.*;
public class DirListing {
                     public static void main(String[] args) {
                             if (args.length < 1) {
                                      System.out.println("Usage: DirListing DirectoryName");
                                      System.exit(0);                             }                             Path dirPath = Paths.get(args[0]);                             DirectoryStream<Path> directory = null;
                             try {                                      directory = Files.newDirectoryStream(dirPath);
                                      for (Path p : directory) {                                                System.out.println(p.getFileName());
                                      }                             } catch (Exception ie) {                                      System.out.println("Invalid path specified:" + args[0]);
                             } finally {                                      try {
                                                if (directory != null) {                                                          directory.close();                                                }                                      } catch (IOException ie) {                                                ie.printStackTrace();
                                      }                             }                     }
}
```

[Explanation](https://yourviews.mindstick.com/view/84608/what-are-hindenburg-s-allegations-against-adani-detailed-explanation)

- Note that at the top, we import classes from the java.nio.file package. After confirming that the user has input an argument on the command line, we construct a Path object by calling the static get method of the Paths class:

Path dirPath = Paths.get(args[0]);

- After this, we declare a directory variable of type DirectoryStream, as follows(This declaration uses generics):

DirectoryStream<Path> directory = null;

- We initialize this variable by calling the newDirectoryStream method of the Files class on the previously created dirPath object:

directory = Files.newDirectoryStream(dirPath);

- Now, we can print the names of all the files from this directory stream using a [foreach loop](https://www.mindstick.com/blog/298/parellel-foreach-loop), as follows:

```
for (Path p : directory) {
System.out.println(p.getFileName());
} 
```

In the [finally block](https://www.mindstick.com/articles/12106/exception-handling-in-java-guidelines-on-the-use-of-finally-block), we close the opened stream. We can run the program by specifying a desired path on the command line, and we will see the list of files printed to your console.

##### Filtering the Directory Listing

In the above example, all the files present in the specified directory are listed on the console. With a little modification to this program, we could filter out the files of a specified type in our output.

For example, we might want to list out only the files with **.doc** extension from the specified directory. We can add such filters easily in our program by making the following modification in the [initialization](https://www.mindstick.com/interview/160/what-is-the-difference-between-assignment-and-initialization) of the directory object:

- directory = dir.newDirectoryStream("*.{doc}");

- directory = Files.newDirectoryStream(dirPath, "*.{docx}");

The **newDirectoryStream** method now takes a filter string [as an argument](https://answers.mindstick.com/qa/31668/in-c-if-you-pass-an-array-as-an-argument-to-a-function-what-actually-gets-passed). Thus, when we retrieve elements from the created stream, we get only the files with the extension .doc. We can create our own filter, as shown in the following example, which returns files having a size greater than 8,192 bytes:

```
DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
         public boolean accept(Path file) throws IOException {
                                   return (Files.size(file) > 8192L);          }};
```

To use this filter while listing the files in the directory, we would use the following code:

**directory = Files.newDirectoryStream(dirPath, fil**

---

Original Source: https://www.mindstick.com/blog/11125/java-i-o-accessing-the-host-file-system

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
