Open a Dir in Java and Read All

Java files - constructing file and directory names with File, Path, Paths - Feature image

Constructing File and Directory Names (with File, Path, and Paths) [Java Files Tutorial, Part three]

Author image

by Sven WoltmannJanuary 15, 2020

Subsequently roofing file reading and writing operations in Java, this tertiary office of the serial of articles shows how to apply the classes File, Path, and Paths to construct file and directory paths – regardless of the operating arrangement.

If y'all have already dealt with file operations in Java, you lot have probably used these classes to laissez passer a filename to 1 of the read or write operations with new File(), Paths.get() or Path.of().

Well-nigh programmers do non study these classes in much more particular. This is partly because these classes can be confusing even for experienced Java programmers: What is the difference between File.getName() and File.getPath()? What is the difference between File.getAbsolutePath(), File.getCanonicalPath() and Path.normalize()? What is the difference between Paths.go() and Path.of()?

This article answers the following questions:

  • What is the deviation between a filename, a directory proper noun, and a path?
  • How to construct a relative directory or file path independent of the operating system?
  • How to construct an accented directory or file path contained of the operating system?
  • What changed with the introduction of the NIO.2 File API?
  • What exactly is returned past the File methods getName(), getPath(), getParent(), getParentFile(), getAbsolutePath(), getAbsoluteFile(), getCanonicalPath(), getCanonicalFile()?
  • What is returned past the Path methods getFileName(), getName(int index), getParent(), getRoot(), toAbsolutePath() und normalize()?
  • How to join Path objects with Path.resolve() and Path.resolveSibling()?
  • When to use File and when to utilize Path? And tin can I catechumen the two into each other?

Basics: Definitions of terms, operating system independence, NIO.2

What is the difference betwixt filename, directory proper name, relative path, and absolute path?

Before we get-go, we need to agree on terminology. For instance, the terms "path" and "directory" are oftentimes confused.

  • Filename: the name of a file without a directory and separators, eastward.g., readme.txt
  • Directory name: the name of a particular directory without parent directory(due south), for example, apache-maven-iii.6.iii or log
  • Path: the "route" to an object of the file system, i.eastward., to files and directories. A path can exist absolute or relative:
    • An accented path is always unique and independent of the current position in the file system.
    • A relative path is related to the current position within the file system. It describes how to get from this position to the target. If the target is located in the current directory, the relative path usually equals the file or directory proper name.

The following table shows some examples of accented and relative paths to directories and files – for both Windows and Linux/Mac:

Linux / Mac Windows
Accented path to a directory: /var/log C:\Windows
Accented path to a file: /var/log/syslog C:\Windows\explorer.exe
Relative path to a directory: ../../var/log

(from /domicile/user to /var/log)

..\Windows

(from C:\Users to C:\Windows)

Relative path to a file: ../../var/log/syslog

(from /home/user to /var/log/syslog)

..\Windows\explorer.exe

(from C:\Users to C:\Windows\explorer.exe)

Operating system independent path and directory names

As seen in the table, absolute paths on Windows start with a drive letter and a colon; directories are separated by a backslash ('\'). On Linux and Mac, absolute paths start with a forward slash ('/'), which also separates directories.

Yous tin admission the separator of the currently used operating system with the constant File.separator or the method FileSystems.getDefault().getSeparator() and thus generate pathnames "manually" (i.east. past concatenating strings). For example like this:

              

String homePath = System.getProperty("user.habitation"); String fileName = "test" + System.currentTimeMillis(); String filePath = homePath + File.separator + fileName; Organization.out.println("filePath = " + filePath);

Code language: Java ( java )

Depending on the operating system we get a different output:

  • Windows: filePath = C:\Users\svenw\test1578950760671
  • Linux: filePath = /abode/sven/test1578950836130

With the home directory, this works quite well, but with the temp directory (we go this from the system property "coffee.io.tmpdir") we go the following output:

  • Windows: filePath = C:\Users\svenw\AppData\Local\Temp\\test1578950862590
  • Linux: filePath = /tmp/test1578950884314

Did you spot the problem?

On Windows, the path for the temporary directory already ends with a backslash. By calculation a separator, our lawmaking creates a double backslash in the file path.

We could at present check if the directory proper noun already ends with a separator and add it only if it is non present. But that is not necessary at all. You lot can construct file and directory names much more elegantly – completely without string operations, using the classes coffee.io.File and (from Java 7) coffee.nio.file.Path.

"Old" Java File API vs. NIO.two File API

In Java 7, the "NIO.2 File API" was introduced with JSR 203 (NIO stands for "New I/O"). This provides a whole new set of classes for handling files (introduced in the previous article nearly writing and reading files).

Files and directories were previously represented by the class java.io.File. This leads to confusion, specially for beginners, considering the class name suggests that the class only represents files, non directories.

In NIO.2, this task is taken over by the – now appropriately named – class java.nio.file.Path. Its interface has been completely rewritten compared to coffee.io.File.

Constructing file and directory paths with coffee.io.File

Permit's start with the "former" form, java.io.File. A file object can represent a filename, a directory name, a relative file or directory path, or an absolute file or directory path (where a file/directory name is actually also a relative file/directory path, relative to the directory in which the file/directory is located).

java.io.file: File and directory names

Representing file names with coffee.io.File

You can define a filename (without specifying a directory) as follows (we stick to the pattern "exam<timestamp>" used above):

              

File file = new File("exam" + System.currentTimeMillis());

Lawmaking language: Coffee ( coffee )

You lot can now read the post-obit information from the File object:

Method Render value
file.getName() test1578953190701
file.getPath() test1578953190701
file.getParent() / file.getParentFile() null
file.getAbsolutePath() / file.getAbsoluteFile() /happycoders/git/filedemo/test1578953190701

The method getName() returns the filename we passed to the constructor. getPath() returns the path, which in this instance corresponds to the filename since we accept non specified a directory. For the same reason, getParent() and getParentFile() both render null, the outset method returns a String, the second a corresponding File object.

Using the methods getAbsolutPath() and getAbsolutFile(), you map this file into the current working directory and go the complete path of the file including its directory and filename. These two methods also differ but in that the outset returns a String, and the 2nd returns a corresponding File object.

There are as well the methods getCanonicalPath() and getCanonicalFile(), which would return the same values every bit getAbsolutePath() and getAbsoluteFile() in this and the following examples. We will see in a later instance, in which cases they tin can contain other values.

Representing directory names with java.io.File

The File object constructed in the previous department could just also stand for a directory with the same name instead of a file. The methods listed in the table in a higher place would return the same results.

A distinction would only be possible if a file or directory with this proper name already exists. If a corresponding file exists, the method file.isFile() returns true. If, on the other hand, a directory with this proper name exists, the method file.isDirectory() returns true. If neither file nor directory exists, both methods render false. Depending on the further use, the File object tin can then be used either to create a directory or to create a file.

java.io.File: Relative file and directory paths

Relative file path with coffee.io.File

To specify a directory, we tin can pass it to the File constructor as a parameter – in the simplest form as a String. With the following code, you put the test file into a tests directory:

              

File file = new File("tests", "examination" + System.currentTimeMillis());

Code language: Java ( java )

The getters now provide the following information about the File object (with the differences to the previous example highlighted in assuming):

Method Return value
file.getName() test1578953190701
file.getPath() tests/test1578953190701
file.getParent() / file.getParentFile() tests
file.getAbsolutePath() / file.getAbsoluteFile() /happycoders/git/filedemo/tests/test1578953190701

We can now run into a difference between getName() and getPath(): the first method returns only the file name without the directory information, the second method returns the consummate relative path. getParent() and getParentFile() (remember: the beginning method returns a Cord, the second a respective File object) now return the specified directory tests. In the accented path information returned by getAbsolutePath() and getAbsoluteFile() (again: String vs. File), the subdirectory tests gets inserted accordingly.

The directory can also be passed as a File object instead of a String:

              

File directory = new File("tests"); File file = new File(directory, "test" + Arrangement.currentTimeMillis());

Code language: Java ( java )

Relative file path with nested directories

Several directory levels can also be nested:

              

File testsDirectory = new File("tests"); File yearDirectory = new File(testsDirectory, "2020"); File dayDirectory = new File(yearDirectory, "2020-01-13"); File file = new File(dayDirectory, "test" + System.currentTimeMillis());

Code language: Java ( coffee )

This allows united states to construct directory paths of any depth without having to use the separator character even one time. The example constructs a file object with the path tests/2020/2020-01-xiii/test1578953190701.

What volition the getParent() method return now? tests/2020/2020-01-13 or simply 2020-01-13? Let's try it…

Method Render value
file.getName() test1578953190701
file.getPath() tests/2020/2020-01-13/test1578953190701
file.getParent() / file.getParentFile() tests/2020/2020-01-13
file.getAbsolutePath() / file.getAbsoluteFile() /happycoders/git/filedemo/tests/2020/2020-01-13/test1578953190701

The parent, therefore, represents the path to the parent directory, not just its proper name. To access the proper noun of the parent directory we can use file.getParentFile().getName().

Relative directory path with java.io.File

Also, in the examples from the previous section, test1578953190701 could be a directory instead of a file. The path does non allow any conclusions to exist drawn.

coffee.io.File: Absolute file and directory paths

Absolute directory path with java.io.File

Attention: We look at absolute paths in opposite order: Nosotros showtime construct the directory path and then the file path, since we cannot generate an absolute file path without an accented directory path equally a parent.

(There is one exception, which we accept already seen in the previous examples: We tin obtain the accented file path for a file in the current directory by invoking the File constructor with only the file proper name and so calling the method getAbsoluteFile() / getAbsolutePath() on the created File object.)

We accept the following options for constructing an absolute directory path:

  • from an absolute directory path represented as a String (therefore dependent on the operating arrangement)
  • from the system properties mentioned at the first, like "user.home" and "coffee.io.tmpdir"
  • from the current directory

Amalgam an absolute directory path from a String

In one case we take the absolute directory path in a Cord (for example, from a configuration file), we tin can pass information technology direct to the File constructor. The following case uses a Cord constant for simplicity:

              

File directory = new File("/var/log/myapp");

Lawmaking language: Java ( java )

For this absolute directory, the File object'south getters render the following values:

Method Return value
file.getName() myapp
file.getPath() /var/log/myapp
file.getParent() / file.getParentFile() /var/log
file.getAbsolutePath() / file.getAbsoluteFile() /var/log/myapp

The Methods getPath(), getAbsolutePath() and getAbsoluteFile() now all return the accented path of the directory. getParent() and getParentFile() render the accented path of the parent directory.

Amalgam an accented directory path from arrangement properties

Through the system properties user.home and java.io.tmpdir, you lot get – independent of the operating system – the user's dwelling house directory and the temporary directory.

The System.getProperty() method finally returns the path as a String, which nosotros tin and then pass to the File constructor. Above, we saw that on Windows, the temporary directory has a trailing backslash, the home directory does not. Does that cause us problems at this point?

Nosotros can use the following code to test it on Windows:

              

String homeDir = System.getProperty("user.domicile"); Organisation.out.println("homeDir = " + homeDir); Organization.out.println("homeDir every bit File object = " + new File(homeDir)); String tempDir = System.getProperty("java.io.tmpdir"); System.out.println("tempDir = " + tempDir); System.out.println("tempDir as File object = " + new File(tempDir));

Code language: Coffee ( java )

The programme delivers the following output:

              

homeDir = C:\Users\svenw homeDir as File object = C:\Users\svenw tempDir = C:\Users\svenw\AppData\Local\Temp\ tempDir as File object = C:\Users\svenw\AppData\Local\Temp

Code language: plaintext ( plaintext )

The unnecessary abaft backslash has been removed, so we don't accept to worry about anything.

Creating an absolute directory path from the current directory

Using what we know so far, we could construct the accented directory path of the electric current directory as follows:

              

File file = new File("dummy"); File absoluteFile = file.getAbsoluteFile(); File absoluteDirectory = absoluteFile.getParentFile();

Lawmaking language: Coffee ( coffee )

Here nosotros have generated a dummy file path, constructed the accented path for it (which maps the file into the electric current directory), and and then extracted the parent – the accented path of the directory in which the file is located.

Admittedly, this is rather cumbersome. Fortunately, there is a more elegant mode: The current directory tin also exist read from a arrangement holding, user.dir, and then passed to the File constructor:

              

File currentDir = new File(System.getProperty("user.dir"));

Code language: Java ( java )

Absolute file path with java.io.File

Afterwards having looked at different ways of creating an absolute directory path, we can now construct an absolute file path. All nosotros have to do is pass the directory and filename to the File constructor.

              

File tempDir = new File(System.getProperty("java.io.tmpdir")); File subDir = new File(tempDir, "myapp"); String fileName = "foo"; File file = new File(subDir, fileName);

Code linguistic communication: Coffee ( coffee )

For this example, the getters of the File object render the following values:

Method Return value
file.getName() foo
file.getPath() /tmp/myapp/foo
file.getParent() / file.getParentFile() /tmp/myapp
file.getAbsolutePath() / file.getAbsoluteFile() /tmp/myapp/foo

We see a similar pattern every bit in the instance with the absolute directory path /var/log/myapp: The methods getPath(), getAbsolutePath() and getAbsoluteFile() return the absolute path of the file. And getParent() and getParentFile() return the absolute path of the parent directory.

Attention:

If y'all create a file object for a file in the current directory, it makes a divergence whether yous create the object with the electric current directory and filename or just the filename. The methods getAbsoluteFile() / getAbsolutePath() return the absolute file path in both cases. Nevertheless, getPath() returns the accented path only in the first case; whereas in the second case it returns merely the filename. And getParent() and getParentFile() but return the parent directory in the kickoff instance, merely aught in the 2nd example.

java.io.file: Overview of File's getter methods

The following table summarizes what File's getters return depending on the file system object represented:

Method File/directory name Relative file/directory path Absolute file/directory path
getName() File/directory name File/directory proper name File/directory name
getPath() File/directory name Relative file/directory path Absolute file/directory path
getParent() / getParentFile() zero Relative path to parent directory Absolute path to parent directory
getAbsolutePath() / getAbsoluteFile() Absolute path from combination of current directory and file/directory name Accented path from combination of current directory and relative file/directory path Accented file/directory path

Once again as a reminder: getParent() and getAbsolutePath() return a String; getParentFile() and getAbsoluteFile() return a corresponding File object.

java.io.file: What is the difference between getCanonicalPath() / getCanonicalFile() and getAbsolutePath() / getAbsolutePath()?

In all previous examples, the methods getCanonicalPath() and getCanonicalFile() would take returned the same result as getAbsolutePath() and getAbsoluteFile() – namely the respective absolute path (every bit Cord or File object).

And then what is the difference?

A "approved path" is unique; i.e., there is merely one such path to a file. In dissimilarity, there can be more than ane accented path to the same file. An case:

For the file /var/log/syslog, this Cord is also the "canonical path". The same String is likewise an accented path. However, there are other absolute paths, such as

  • /var/log/./syslog,
  • /var/log/../log/syslog,
  • /home/user/../../var/log/syslog,
  • too as all paths that eventually point to /var/log/syslog via symbolic links.

Constructing file and directory paths with java.nio.file.Path and Paths

Although the interface of java.nio.file.Path has been completely changed from coffee.io.File, the underlying concepts take remained unchanged. Filenames, directory names, relative file and directory paths, and accented file and directory paths are even so the file organisation objects being represented.

What was changed? In the post-obit sections, you see how to construct Path objects, using the same structure and the same examples every bit with the File objects before.

In the following sections, I will not make a distinction betwixt file and directory names. We accept already seen to a higher place that, as long as the corresponding file system object does not exist, its path does not point whether information technology is a file or a directory.

java.nio.file.Path: File and directory names

Instead of a constructor, we use a factory method for coffee.nio.file.Path to create instances. The mill method was originally located in the class java.nio.file.Paths (with "s" at the cease), but in Java eleven, it was besides straight included in the Path class. We create a Path object for the file proper name "test<timestamp>" as follows:

              

Path path = Paths.get("examination" + Organization.currentTimeMillis());

Code linguistic communication: Java ( java )

Starting with Java 11, yous can use Path.of() instead of Paths.get(). In that location is practically no difference. Internally, Paths.get() calls the newer method Path.of() and this in turn calls FileSystems.getDefault().getPath().

              

Path path = Path.of("test" + System.currentTimeMillis());

Code linguistic communication: Java ( java )

The following methods provide information analogous to methods of the File form shown above:

Method Return value
path.getFileName() test1579037366379
path.getNameCount() 1
path.getName(0) test1579037366379
path.getParent() naught
path.getRoot() null
path.toAbsolutePath() /happycoders/git/filedemo/test1579037366379
path.normalize() test1579037366379

Starting time of all: All methods shown – except for getNameCount() – return a Path object. There are no variants of these methods that return a Cord. The only style to convert a Path object into a Cord is to telephone call its toString() method.

The getFileName() method returns the name of the file or directory. getNameCount() is always 1 for a filename without directory information, or for directories without a parent directory. And the start name, retrievable past getName(0), is as well the file/directory proper name. getParent() returns – similar File's method with the aforementioned name – nil. The method getRoot() also returns null, since a single filename is always relative. With toAbsolutePath() nosotros map – analogous to File.getAbsolutePath() – the file/directory into the electric current directory and get the corresponding accented path.

Path.normalize() is like to File.getCanonicalPath() with the difference that when normalizing a path, relative paths remain relative, while getCanonicalPath() always generates an absolute path for both absolute and relative File objects.

coffee.nio.file.Path: Relative file and directory paths

To include directories, Path offers two unlike approaches:

  • Yous can listing all directories in the factory method Paths.get() or Path.of().
  • You lot can use its resolve() method to convert an existing Path object into a new Path object, where the resolve() method is equivalent to irresolute the directory with the Windows or Linux cd command.

Constructing relative file and directory paths with Paths.get() / Path.of()

You can pass any number of directory names to the factory methods. For example, you would construct the relative path tests/test1578953190701 as follows (the timestamp is hard-coded for simplicity):

              

Path path = Paths.get("tests", "test1578953190701");

Code language: Java ( java )

This Path object's getters return the following results (I highlighted the differences to the private file names in bold again):

Method Return value
path.getFileName() test1578953190701
path.getNameCount() two
path.getName(0) tests
path.getName(1) test1578953190701
path.getParent() tests
path.getRoot() null
path.toAbsolutePath() /happycoders/git/filedemo/tests/test1578953190701
path.normalize() tests/test1578953190701

Due to the directory, nameCount has increased from 1 to two. In the name array, the directory name was inserted at position 0; the file name has moved to position 1. getParent() also returns the directory proper name. The accented path and the normalized version also incorporate the directory name.

Amalgam nested file and directory paths with Paths.get() / Path.of()

If the file is to exist located in the directory tests/2020/2020-01-13, call the factory method as follows:

              

Path path = Paths.get("tests", "2020", "2020-01-13", "test1578953190701");

Code language: Java ( java )

Here once more, the getters' results:

Method Return value
path.getFileName() test1578953190701
path.getNameCount() iv
path.getName(0) tests
path.getName(1) 2020
path.getName(2) 2020-01-13
path.getName(3) test1578953190701
path.getParent() tests/2020/2020-01-13
path.getRoot() null
path.toAbsolutePath() /happycoders/git/filedemo/tests/2020/2020-01-13/test1578953190701
path.normalize() tests/2020/2020-01-13/test1578953190701

nameCount has been increased accordingly, and the name array contains all directories of the path as well as the filename. Again, getParent() returns the complete known path to the directory, not just its name.

Constructing relative file and directory paths with Path.resolve()

An alternative way to construct the Path object for the path tests/2020/2020-01-13/test1578953190701 is to utilize the resolve() method. It combines the path on which the method is called with the path passed to it:

              

Path testsDir = Paths.get("tests"); Path yearDir = testsDir.resolve("2020"); Path dayDir = yearDir.resolve("2020 -01 -thirteen"); Path path = dayDir.resolve("test1578953190701");

Code language: GLSL ( glsl )

The resolve() operation is associative, i.e., the private parts tin also be joined in a different order, for example like this:

              

Path testsDir = Paths.become("tests"); // tests Path yearDir = testsDir.resolve("2020"); // tests/2020 Path dayDir = Paths.get("2020-01-xiii"); // 2020-01-13 // 2020-01-xiii/test1578953190701 Path fileInDayDir = dayDir.resolve("test1578953190701"); // tests/2020/2020-01-xiii/test1578953190701 Path path = yearDir.resolve(fileInDayDir);

Code language: Java ( java )

Shortcut: Path.resolveSibling()

Let's assume we have the Path object nosotros synthetic in the previous department, and we want to create some other file in the same directory. If we notwithstanding have access to the Path object representing the directory, we can call resolve() on it with the new filename. Otherwise, we could admission the directory with getParent() and then call resolve():

              

Path sibling = path.getParent().resolve("test" + System.currentTimeMillis());

Lawmaking language: Java ( java )

Exactly for this purpose, there is the shortcut resolveSibling(), which saves y'all five keystrokes and bytes:

              

Path sibling = path.resolveSibling("test" + System.currentTimeMillis());

Lawmaking language: Coffee ( coffee )

java.nio.file.Path: Absolute file and directory paths

Similar to java.io.File, we can also create an absolute path with java.nio.file.Path from a String that we read from a configuration file or a organization belongings. We tin can laissez passer this path, as it is, to the factory method. We do not need to carve up it up kickoff. Using the directory /var/log/myapp as an example again:

              

Path path = Paths.go("/var/log/myapp");

Code linguistic communication: Coffee ( java )

Of form, we can too modify an absolute path using the resolve() method. The following example corresponds to the example of the absolute file path with java.io.File:

              

Path tempDir = Path.of(Organisation.getProperty("java.io.tmpdir")); Path subDir = tempDir.resolve("myapp"); String fileName = "foo"; Path file = subDir.resolve(fileName);

Code linguistic communication: Coffee ( coffee )

For this absolute path, the getter methods return:

Method Return value
path.getFileName() foo
path.getNameCount() 3
path.getName(0) tmp
path.getName(1) myapp
path.getName(2) foo
path.getParent() /tmp/myapp
path.getRoot() /
path.toAbsolutePath() /tmp/myapp/foo
path.normalize() /tmp/myapp/foo

Here nosotros experience for the beginning time that path.getRoot() does not return null, just "/", the Linux root directory. On Windows, nosotros would become "C:\" here (unless the temporary directory is in a different file organization). The root directory is not function of the proper noun array.

coffee.nio.file.Path: Overview of its getter methods

Here you tin can see a summary of what Path's getters return:

Method File/directory proper name Relative file/directory path Absolute file/directory path
path.getFileName() File/directory name File/directory name File/directory name
path.getNameCount() 1 Number of directories + file Number of directories + file
path.getName(index) File/directory proper name File/directory names at the given position (0-based) File/directory names at the given position (0-based, root does not count)
path.getParent() null Relative path to parent directory Absolute path to parent directory
path.getRoot() null zero The file system'due south root, such equally "/" or "C:\"
path.toAbsolutePath() Absolute path from combination of current directory and file/directory name Absolute path from combination of electric current directory and file/directory name Accented file/directory path
path.normalize() Normalized file/directory proper name Normalized relative file/directory name Normalized accented file/directory name

Summary and outlook

This commodity gave a detailed overview of how to construct file and directory paths independent of the operating system using the classes File, Path, and Paths.

My recommendation is to use only the NIO.2 classes Path and Paths from Java 7 on, and from Coffee 11 on, use only Path. If you need a File object for a particular file operation, y'all tin always create one with Path.toFile().

The post-obit parts of the series will deal with the following topics:

  • Directory operations, such as reading the file list of a directory
  • Copying, moving, and deleting files
  • Creating temporary files
  • Read and write structured data with DataOutputStream andDataInputStream

In the later course nosotros come to avant-garde topics:

  • NIO channels and buffers introduced in Java 1.4, to speed up working with large files
  • Retention-mapped I/O for blazing-fast file access without streams
  • File locking, to access the same files in parallel – i.due east., from several threads or processes – without conflicts

Do you have any questions, suggestions, ideas for improvement? Then I would exist pleased with a comment. Practice you lot know others who observe the topic interesting? Then I would be happy if you share the article by using one of the buttons below. Would y'all like to exist informed when the next part appears? So click here to sign upwardly for the HappyCoders newsletter.

parrishclor1977.blogspot.com

Source: https://www.happycoders.eu/java/how-to-construct-file-and-directory-names-with-file-path-paths/

0 Response to "Open a Dir in Java and Read All"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel