• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read drive letter. i.e (c:, d:, e:) when drive is labelled  RSS feed

 
gauravkv gupta
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while simulating <dir> command of DOS, i need to get drive letter of the drive.
For the output:
D:\>cd android-sdks

D:\android-sdks>dir
Volume in drive D is my labeled drive

how i will get the drive letter(D) in my code.
 
Carey Brown
Saloon Keeper
Posts: 2539
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try javax.swing.filechooser.FileSystemView. Although it is in the Swing package this class does not require that it be a part of a Swing application.
 
Carey Brown
Saloon Keeper
Posts: 2539
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading you post again I'm not sure I was answering the right question. What exactly is your input and what are you expecting as output? If you are starting out with a File, you can get the drive letter by looking at the first character of File.getAbsolutePath() [only true for windows of course].
 
gauravkv gupta
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:After reading you post again I'm not sure I was answering the right question. What exactly is your input and what are you expecting as output? If you are starting out with a File, you can get the drive letter by looking at the first character of File.getAbsolutePath() [only true for windows of course].


Yeah you are right. Thanks for the answer. but now i want to get the label of D drive also which is "My Labeled Drive".
How to get the label.
 
Carey Brown
Saloon Keeper
Posts: 2539
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gauravkv gupta wrote:
Carey Brown wrote:After reading you post again I'm not sure I was answering the right question. What exactly is your input and what are you expecting as output? If you are starting out with a File, you can get the drive letter by looking at the first character of File.getAbsolutePath() [only true for windows of course].


Yeah you are right. Thanks for the answer. but now i want to get the label of D drive also which is "My Labeled Drive".
How to get the label.


For the drive label see FileSystemView.
 
Mohamed Sanaulla
Bartender
Posts: 3169
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java 7 has few APIs to get this information.
Each file location/directory location can be represented using instance of Path class and to obtain an instance of Path you need to make use of the Paths.get(location) API.

Each Path instance has a number of components to it: File Name, Parent and Root. In your case you would be looking for a Root element which can be obtained using pathInstance.getRoot()

 
Carey Brown
Saloon Keeper
Posts: 2539
33
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:Java 7 has few APIs to get this information.
Each file location/directory location can be represented using instance of Path class and to obtain an instance of Path you need to make use of the Paths.get(location) API.

Each Path instance has a number of components to it: File Name, Parent and Root. In your case you would be looking for a Root element which can be obtained using pathInstance.getRoot()



Couldn't quite see how you would get from a Path to a drive label.

This is an Fsv class that wraps around the FileSystemView class. Its main method just prints out all of the root file systems. The output I get looks something like this: (Only better. The post really screwed up the columns.)



 
Mohamed Sanaulla
Bartender
Posts: 3169
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote: Couldn't quite see how you would get from a Path to a drive label.

There are a few new APIs added as part of the Java 7 NIO2 enhancements and java.nio.file.Path, java.nio.file.Paths, FileSystem and FileSystems are one among them. They allow you to access the details of the underlying file system. java.file.nio.Paths is used to get an instance of java.nio.file.Path and FileSystems is used to get an instance of FileSystem. If the OP is using Java 7 I suggested to look at these APIs.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!