Sunday, 16 June 2019

NIO2: Get FileSystem from the File system provider

‘getFileSystem()’ method of FileSystemProvider class is used to get an instance of FileSystem.

Example
FileSystem fileSystem = fileSystemProviders.get(0).getFileSystem(URI.create("file:///"));

App.java
package com.sample.app;

import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.spi.FileSystemProvider;
import java.util.List;

public class App {

 public static void main(String args[]) {
  List<FileSystemProvider> fileSystemProviders = FileSystemProvider.installedProviders();

  FileSystem fileSystem = fileSystemProviders.get(0).getFileSystem(URI.create("file:///"));

  Iterable<Path> paths = fileSystem.getRootDirectories();

  for (Path path : paths) {
   System.out.println(path);
  }
 }
}


Previous                                                 Next                                                 Home

No comments:

Post a Comment