Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between C: and C:/

Tags:

java

windows

I was just reading some java book and making some small programs for practice, I created a small code to get information about the path I entered, and the code is:

String path = JOptionPane.showInputDialog("Enter Path to analyze");

File file =  new File(path);

if (file.exists())
{
    String result = "";
    if (file.isDirectory())
    {
        result += "Path is directory\n ";
        String [] resList = file.list();

        for (String s : resList)
        {
            result += s + ", ";
        }
    }
    if (file.isFile())
    {
        result += "Path is a file\n";
    }

    JOptionPane.showMessageDialog(null, result);

Now in the input dialogue, when I enter C:, the result is build, build.xml, manifest.mf, nbproject, src, but when I enter C:/, it shows the complete list of directories and files in C.

And strangely it does not happen with the D drive and other drives (i.e. the result is same for D:/ and D:), what is happening please explain?

Update Same happens in WPF using C#!

like image 350
SpeedBirdNine Avatar asked Apr 26 '12 18:04

SpeedBirdNine


2 Answers

C: means "whatever directory is currently selected on drive C:". In your case, it's probably the directory that your application is running from.

D: is the same as D:/ in your case because the root directory is the current working directory in D:.

like image 142
sblom Avatar answered Sep 25 '22 14:09

sblom


This is not really a java question, but a windows/dos question.

The explanation comes down to the old dos command for switching drives.

Typing a drive letter followed by a colon is a command to change drives in dos, therefore the 'command' C: does nothing since your working dir is already on the C drive. The 'directory' returned by the native interface to the JRE is the same as if you used the path "", ie your working directory.

On the other hand, add a slash and it is a proper path, to the root of your C drive, therefore your JRE is given this directory by the native interface.

If you go to a dos command (windows>run>cmd) and type in C: you will see that it accepts the command but does not change directory, unless of course you are currently on a different drive at the time.

hope that helps.

like image 21
pstanton Avatar answered Sep 25 '22 14:09

pstanton