Thursday 10, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Tuesday, 23 May 2017

How to retrieve drive/root directories from system using Java

File[] java.io.File.listRoots() 
List the available filesystem roots.

A particular Java platform may support zero or more hierarchically-organized file systems. Each file system has a root directory from which all other files in that file system can be reached. Windows platforms, for example, have a root directory for each active drive; UNIX platforms have a single root directory, namely "/". The set of available filesystem roots is affected by various system-level operations such as the insertion or ejection of removable media and the disconnecting or unmounting of physical or virtual disk drives.

This method returns an array of File objects that denote the root directories of the available filesystem roots. It is guaranteed that the canonical pathname of any file physically present on the local machine will begin with one of the roots returned by this method.

The canonical pathname of a file that resides on some other machine and is accessed via a remote-filesystem protocol such as SMB or NFS may or may not begin with one of the roots returned by this method. If the pathname of a remote file is syntactically indistinguishable from the pathname of a local file then it will begin with one of the roots returned by this method. Thus, for example, File objects denoting the root directories of the mapped network drives of a Windows platform will be returned by this method, while File objects containing UNC pathnames will not be returned by this method.

Unlike most methods in this class, this method does not throw security exceptions. If a security manager exists and its SecurityManager.checkRead(String) method denies read access to a particular root directory, then that directory will not appear in the result.

Returns:
An array of File objects denoting the available filesystem roots, or null if the set of roots could not be determined. The array will be empty if there are no filesystem roots.

Since:
1.2

See Also:
java.nio.file.FileStore

SystemDrives.java
01.package com.javatutorialcorner.directory;
02. 
03.import java.io.File;
04. 
05.public class SystemDrives {
06. 
07. public static void main(String[] args) {
08.  getSystemDrives();
09. 
10. }
11. 
12. private static void getSystemDrives() {
13.  File[] drives = File.listRoots();
14.  System.out.println("Drive/Root directories in your system :");
15. 
16.  for (int i = 0; i < drives.length; i++) {
17.   System.out.println(drives[i].toString());
18.  }
19. 
20. }
21. 
22.}
Output

Drive/Root directories in your system :
C:\

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to delete directory using Java boolean java.io.File.delete() Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. Note that the java.nio.file.Fil… Read More
  • Read File From Class Path or Resource Folder In this article we are going to see about how to read file from class path or from resource folder. Content in javatutorialcorner.txt Spring Hibernate Core Java Java 8 Java 7 Using ClassLoader(), all paths are "absolut… Read More
  • How to retrieve present working directory using Java CurrentWorkingDirectory.java package com.javatutorialcorner.directory; public class CurrentWorkingDirectory { public static void main(String[] args) { getCurrentWorkingDirectory(); } private static void getCurrentWo… Read More
  • Java File Examples Java File Examples Reading the file using FileInputStream Read File using BufferedReader Read File using BufferedInputStream How to Write File using BufferedWriter How to write file using FileOutputStream in Java How to a… Read More
  • Java Directory Examples How to Create Directory How to retrieve all the files from Directory How to search file from directory How to retrieve all the files from Directory using File.listFiles() How to retrieve all the directory names How to retr… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to retrieve drive/root directories from system using Java Rating: 5 Reviewed By: eHowToNow