Showing posts with label nio2. Show all posts
Showing posts with label nio2. Show all posts

Sunday, 16 June 2019

NIO2: Introducing FileSystemProvider class

FileSystemProvider is a Service-provider class for file systems.

This is an abstract class. All the concrete file system provider implementations must implement the abstract methods of this class.

A provider is identified by the scheme. Below snippet return all the filesystem providers that are installed in your system.

App.java
package com.sample.app;

import java.io.IOException;
import java.nio.file.spi.FileSystemProvider;
import java.util.List;

public class App {

    public static void main(String args[]) throws IOException {

        List<FileSystemProvider> fileSystemProviders = FileSystemProvider.installedProviders();

        for (FileSystemProvider fileSystemProvider : fileSystemProviders) {
            System.out.println(fileSystemProvider.getScheme());
        }

    }
}


Output

file
jar



Previous                                                 Next                                                 Home

NIO2 Introduction


NIO2 is introduced in Java7 and these APIs are used to access the file system directly.

The APIs provided by java standard IO library are used to perform file operations like read content, write to content, traverse directory, move file etc., then why NIO2? It is because, standard IO libraries are not efficient in handling directories with tons of files. NIO2 solves the performance problem by directly plugged into native file system.

There are 4 main classes in NIO2.
a.   FileSystemProvider
b.   FileStore
c.    FileSystem
d.   FileSystems

Previous                                                 Next                                                 Home