Java list file systems

Java list file systems

Factory methods for file systems. This class defines the getDefault method to get the default file system and factory methods to construct other types of file systems. The first invocation of any of the methods defined by this class causes the default provider to be loaded. The default provider, identified by the URI scheme «file», creates the FileSystem that provides access to the file systems accessible to the Java virtual machine. If the process of loading or initializing the default provider fails then an unspecified error is thrown. The first invocation of the installedProviders method, by way of invoking any of the newFileSystem methods defined by this class, locates and loads all installed file system providers. Installed providers are loaded using the service-provider loading facility defined by the ServiceLoader class. Installed providers are loaded using the system class loader. If the system class loader cannot be found then the platform class loader is used. Providers are typically installed by placing them in a JAR file on the application class path, the JAR file contains a provider-configuration file named java.nio.file.spi.FileSystemProvider in the resource directory META-INF/services , and the file lists one or more fully-qualified names of concrete subclass of FileSystemProvider that have a zero argument constructor. The ordering that installed providers are located is implementation specific. If a provider is instantiated and its getScheme returns the same URI scheme of a provider that was previously instantiated then the most recently instantiated duplicate is discarded. URI schemes are compared without regard to case. During construction a provider may safely access files associated with the default provider but care needs to be taken to avoid circular loading of other installed providers. If circular loading of installed providers is detected then an unspecified error is thrown. This class also defines factory methods that allow a ClassLoader to be specified when locating a provider. As with installed providers, the provider classes are identified by placing the provider configuration file in the resource directory META-INF/services . If a thread initiates the loading of the installed file system providers and another thread invokes a method that also attempts to load the providers then the method will block until the loading completes.

Method Summary

Methods declared in class java.lang.Object

Method Detail

getDefault

Returns the default FileSystem . The default file system creates objects that provide access to the file systems accessible to the Java virtual machine. The working directory of the file system is the current user directory, named by the system property user.dir . This allows for interoperability with the java.io.File class. The first invocation of any of the methods defined by this class locates the default provider object. Where the system property java.nio.file.spi.DefaultFileSystemProvider is not defined then the default provider is a system-default provider that is invoked to create the default file system. If the system property java.nio.file.spi.DefaultFileSystemProvider is defined then it is taken to be a list of one or more fully-qualified names of concrete provider classes identified by the URI scheme «file» . Where the property is a list of more than one name then the names are separated by a comma. Each class is loaded, using the system class loader, and instantiated by invoking a one argument constructor whose formal parameter type is FileSystemProvider . The providers are loaded and instantiated in the order they are listed in the property. If this process fails or a provider’s scheme is not equal to «file» then an unspecified error is thrown. URI schemes are normally compared without regard to case but for the default provider, the scheme is required to be «file» . The first provider class is instantiated by invoking it with a reference to the system-default provider. The second provider class is instantiated by invoking it with a reference to the first provider instance. The third provider class is instantiated by invoking it with a reference to the second instance, and so on. The last provider to be instantiated becomes the default provider; its getFileSystem method is invoked with the URI «file:///» to get a reference to the default file system. Subsequent invocations of this method return the file system that was returned by the first invocation.

Читайте также:  Synchronized java под капотом

getFileSystem

Returns a reference to an existing FileSystem . This method iterates over the installed providers to locate the provider that is identified by the URI scheme of the given URI. URI schemes are compared without regard to case. The exact form of the URI is highly provider dependent. If found, the provider’s getFileSystem method is invoked to obtain a reference to the FileSystem . Once a file system created by this provider is closed it is provider-dependent if this method returns a reference to the closed file system or throws FileSystemNotFoundException . If the provider allows a new file system to be created with the same URI as a file system it previously created then this method throws the exception if invoked after the file system is closed (and before a new instance is created by the newFileSystem method). If a security manager is installed then a provider implementation may require to check a permission before returning a reference to an existing file system. In the case of the default file system, no permission check is required.

newFileSystem

public static FileSystem newFileSystem​(URI uri, MapString,​?> env) throws IOException

Constructs a new file system that is identified by a URI This method iterates over the installed providers to locate the provider that is identified by the URI scheme of the given URI. URI schemes are compared without regard to case. The exact form of the URI is highly provider dependent. If found, the provider’s newFileSystem(URI,Map) method is invoked to construct the new file system. Once a file system is closed it is provider-dependent if the provider allows a new file system to be created with the same URI as a file system it previously created. Usage Example: Suppose there is a provider identified by the scheme «memory» installed:

Map env = new HashMap<>(); env.put("capacity", "16G"); env.put("blockSize", "4k"); FileSystem fs = FileSystems.newFileSystem(URI.create("memory:///?name=logfs"), env);

newFileSystem

public static FileSystem newFileSystem​(URI uri, MapString,​?> env, ClassLoader loader) throws IOException

Constructs a new file system that is identified by a URI This method first attempts to locate an installed provider in exactly the same manner as the newFileSystem(URI,Map) method. If none of the installed providers support the URI scheme then an attempt is made to locate the provider using the given class loader. If a provider supporting the URI scheme is located then its newFileSystem(URI,Map) is invoked to construct the new file system.

Читайте также:  Styling parent element css

newFileSystem

public static FileSystem newFileSystem​(Path path, ClassLoader loader) throws IOException

Constructs a new FileSystem to access the contents of a file as a file system. This method makes use of specialized providers that create pseudo file systems where the contents of one or more files is treated as a file system. This method iterates over the installed providers. It invokes, in turn, each provider’s newFileSystem(Path,Map) method with an empty map. If a provider returns a file system then the iteration terminates and the file system is returned. If none of the installed providers return a FileSystem then an attempt is made to locate the provider using the given class loader. If a provider returns a file system then the lookup terminates and the file system is returned.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Java Files.list

Java Files.list tutorial shows how to list files in Java with Files.list .

Files.list returns a lazily populated stream of directory elements. The listing is not recursive.

The elements of the stream are Path objects.

Files.list current directory

The first example lists the current directory.

package com.zetcode; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class FilesListEx < public static void main(String[] args) throws IOException < Files.list(Paths.get(".")) .forEach(path ->System.out.println(path)); > >

The dot symbol represents the current working directory. We get the path object with Paths.get .

Читайте также:  Python visual studio code обучение

Files.list directories

The following example lists directories in the user’s home directory.

package com.zetcode; import java.io.File; import java.io.IOException; import java.nio.file.Files; public class FilesListEx2 < public static void main(String[] args) throws IOException < var homeDir = System.getProperty("user.home"); Files.list(new File(homeDir).toPath()) .filter(path ->path.toFile().isDirectory()) .forEach(System.out::println); > >

We convert the path object to a File with toFile and call the isDirectory method. The stream is filtered with filter .

Files.list by file extensions

The next program lists all PDF files.

package com.zetcode; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class FilesListEx3 < public static void main(String[] args) throws IOException < var homeDir = System.getProperty("user.home") + System.getProperty("file.separator") + "Downloads"; Files.list(Paths.get(homeDir)).filter(path ->path.toString().endsWith(".pdf")) .forEach(System.out::println); > >

The program lists PDF files in the Downloads directory. The path object is converted to a string and we call endsWith on the string to check if it ends with pdf extension.

Files.list count files

We count the number of PDF files.

package com.zetcode; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; public class FilesListEx4 < public static void main(String[] args) throws IOException < var homeDir = System.getProperty("user.home") + System.getProperty("file.separator") + "Downloads"; var nOfPdfFiles = Files.list(Paths.get(homeDir)).filter(path ->path.toString() .endsWith(".pdf")).count(); System.out.printf("There are %d PDF files", nOfPdfFiles); > >

The number of files is determined with count .

In this article we have used Files.list to list the directory contents.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

Источник

Оцените статью