• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11148
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11148
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11148
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11148
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic