How to Know the File Store Type in Java?


In Java, the java.nio.file.FileStore class represents a storage pool, device, partition, volume, or other implementation-specific means of file storage. The FileStore class provides methods for querying information about the storage device, such as its total and available space, its file system type, and whether it supports certain features like file attributes or symbolic links.

The type() method of the FileStore class returns a string representing the file store type. The file store type is a string that identifies the type of file system used by the file store. Examples of file system types include "NTFS" for the Windows NT file system, "ext4" for the fourth extended file system used by Linux, and "HFS+" for the Hierarchical File System used by macOS.

Let's start!

For instance

Suppose the source file be:

" C:/Users/SAMPLE /Desktop/Tutorial/Program/example.txt”

After performing the operation to get the store type, the result will be:

File store type: NTFS

Algorithm

Step-1: Declare and initialize the source file path.

Step-2: Create the FileStore object.

Step-3: Use the type() method to get the store type of file.

Step-4: Print the result.

Syntax

getFileStore(): It is a method defined in the java.nio.file.Path class. It is used to obtain the FileStore object representing the file store or partition that the file referred to by the Path object resides on.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Method

  • By Using User Defined Method

Let's see the program along with its output one by one.

Approach-1: By Using Static Method

In this approach, we will assign the default file system. Then as per the algorithm we will check if a particular file system is open or not in java.

In this approach, path location will be assigned. Then as per the algorithm we will know the file store type in java.

Example

import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main
{
   //main method
   public static void main(String[] args)
   {
      //getting the file path
      Path path = 
Paths.get("C:/Users/SAMPLE/Desktop/Tutorial/Program/example.txt");
      //try block
      try {
         //getting the FileStore object
         FileStore store = Files.getFileStore(path);
         //getting the file store type 
         String type = store.type();
         
         //print the result
         System.out.println("File store type: " + type);  
      //catch block
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

File store type: NTFS

Approach-2: By Using User Defined Method

In this approach, file path will be assigned. Then call a user defined method by passing the given values and as per the algorithm we will know the file store type in java.

Example

import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main2
{
   //main method
   public static void main(String[] args)
   {
      //calling user defined method
      checkFileStoreType();
   }
   //user defined method
   static void checkFileStoreType()
   {
      
      //getting the file path
      Path path = Paths.get("C:/Users/SAMPLE/Desktop/Learn/Program/myfile.txt");
      //try block
      try {  
         //getting the FileStore object
         FileStore store = Files.getFileStore(path);
         
         //getting the file store type 
         String type = store.type();
         
         //print the result
         System.out.println("File store type: " + type);
         
      //catch block
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

File store type: NTFS

In this article, we explored how to know the file store type by using Java programming language.

Updated on: 17-Aug-2023

53 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements