This discussion is archived
14 Replies Latest reply: Aug 7, 2012 8:46 PM by EJP RSS

Java Default FileSystem Root.

851442 Newbie
Currently Being Moderated
If I want to program file System specifiers in a platform generic way in java, I call

System.getProperty("file.separator");

So I know the correct version of slash to use, wherever my program is run.

On Unix, the root of the file system is /
windows, the root is c: by default.

What is the platform independent way to specify or enquire after the
file system root specifier applicable, irrespective of the Operating System
that the present Java program and JVM is on?
  • 1. Re: Java Default FileSystem Root.
    rp0428 Guru
    Currently Being Moderated
    >
    What is the platform independent way to specify or enquire after the
    file system root specifier applicable, irrespective of the Operating System
    that the present Java program and JVM is on?
    >
    It is not clear what information you need or what you are trying to do.

    Can you provide an example of the information you want and how you plan to use it?

    Have you looked at the listRoots() method of the File class? That method will return the available roots. If the roots start with a slash you know that they aren't windows.
    http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html
    >
    The prefix concept is used to handle root directories on UNIX platforms, and drive specifiers, root directories and UNC pathnames on Microsoft Windows platforms, as follows:

    •For UNIX platforms, the prefix of an absolute pathname is always "/". Relative pathnames have no prefix. The abstract pathname denoting the root directory has the prefix "/" and an empty name sequence.

    •For Microsoft Windows platforms, the prefix of a pathname that contains a drive specifier consists of the drive letter followed by ":" and possibly followed by "\" if the pathname is absolute. The prefix of a UNC pathname is "\\"; the hostname and the share name are the first two names in the name sequence. A relative pathname that does not specify a drive has no prefix.
  • 2. Re: Java Default FileSystem Root.
    851442 Newbie
    Currently Being Moderated
    I am indeed using listRoots().

    My question is, that usually c: or / is the root of the file system.

    If it is a windows system, c: might be renamed.
    If listRoots() returns an array longer than one,
    is the listRoots()[0] position what one wants?
    Will is always be the first position of that array that one wants,
    or not? (what happens if c: is renamed a:, as silly as what would be) ?
  • 3. Re: Java Default FileSystem Root.
    rp0428 Guru
    Currently Being Moderated
    You need to reread my reply and answer the questions that were ask.

    No one can help you if they don't know what you are trying to do.
    >
    My question is, that usually c: or / is the root of the file system.
    >
    Incorrect - it may be the root of 'a' file system but there may be D, E or other drives, UNC drives, remote drives, NFS drives. There might be a lot of file systems and each one has a root. That is why listRoots returns an array.
    >
    If listRoots() returns an array longer than one,
    is the listRoots()[0] position what one wants?
    >
    You are the only one that knows what one you want.
  • 4. Re: Java Default FileSystem Root.
    DrClap Expert
    Currently Being Moderated
    rp0428 wrote:
    You are the only one that knows what one you want.
    This might be an over-estimate of the number of people who know what the OP wants.
  • 5. Re: Java Default FileSystem Root.
    rp0428 Guru
    Currently Being Moderated
    >
    This might be an over-estimate of the number of people who know what the OP wants.
    >
    Nothing proprietary about my method. Generate a random number between 0 and 1 and round the results.

    Perhaps I should have truncated.
  • 6. Re: Java Default FileSystem Root.
    851442 Newbie
    Currently Being Moderated
    Is the default hard drive for the root of the file system always

    File.listRoots()[0];

    ?

    Is there any other way to categorically tell where the root of the default file system is,

    since System.getProperty("..."); doesn't seem to have anything?
  • 7. Re: Java Default FileSystem Root.
    rp0428 Guru
    Currently Being Moderated
    If you want the default FileSystem use the getDefault() method of the FileSystem class
    http://openjdk.java.net/projects/nio/javadoc/java/nio/file/FileSystems.html#getDefault()

    Then you can use the FileSystem getRootDirectories method to iterate them.
  • 8. Re: Java Default FileSystem Root.
    851442 Newbie
    Currently Being Moderated
    I am using java 1.6.0_31-b05,

    which does not have a FileSystem class.

    apart from File.listRoots()[0],

    are there any better generic options under my circumstances?
  • 9. Re: Java Default FileSystem Root.
    rp0428 Guru
    Currently Being Moderated
    >
    are there any better generic options under my circumstances?
    >
    No - you don't want to believe that there IS NO default file system and you don't want to tell us what you are trying to do so we can help you find a way to do it.

    Since you don't want to answer our questions we aren't going to waste any more of our time trying to answer yours.
  • 10. Re: Java Default FileSystem Root.
    851442 Newbie
    Currently Being Moderated
    in Java 1.6.0_25, there exists a platform independent way to
    ask and obtain the correct file separator (which way the slash is supposed to go
    depending on what OS platform the program runs on, assuming flexibility).

    So on windows, if I call File.loadRoots()[0], I typically get C, but if there
    is an a: or a b:, the first position may be different, or infact the c:
    drive letter may not exist.

    So how may I eliminate this inclarity? Is there one call I can make
    to get the default master file system root system for any platform?
  • 11. Re: Java Default FileSystem Root.
    800268 Expert
    Currently Being Moderated
    There isn't a way, so the question is what do you mean by "master file system" (ie the drive on which the OS is installed?) and why do you think you need to know this (ie why not use the user.home, java.io.tmpdir or ask to user to select one)?
  • 12. Re: Java Default FileSystem Root.
    handat Expert
    Currently Being Moderated
    Please answer the question that Walter asked you and define what you mean by "master file system"?

    I for example have a Windows system with the following physical drives C: D: E: P: U:
    The system boots off C: since that's where my MBR is and it contains just a 100MB partition with nothing much in it except for recovery tools and the rest of that harddisk is a hidden ext3 partition, but the Windows directory is located on another harddisk D: while E: is my sdcard device where all my private files are saved to, and my Program Files is located on P: and my user profiles are on U:. Each drive is on a separate physical hdd so in my case which drive would you define as your master file system?
  • 13. Re: Java Default FileSystem Root.
    851442 Newbie
    Currently Being Moderated
    I've been after a generic way to just obtain the windows root hard drive, or the unix root /

    File.listRoots(); A:,B:,C:,D: ...
    System.getProperty("user.home"); C:\.....
    the latter seems to be the best generic way.

    What I want is the home drive letter in one call, in the event "os.name" is Windows.

    user.home is enough, but elaborate.

    Is there a property call to get the root windows drive letter, and no further path, and NOT by using the File object?
  • 14. Re: Java Default FileSystem Root.
    EJP Guru
    Currently Being Moderated
    Moderator action: Locking this futile thread.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points