The difference between C: and C: /

I just read some java book and did some small programs for practice, I created a little code to get information about the path you entered, and the code:

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 dialog when I enter C: result is build, build.xml, manifest.mf, nbproject, src , but when I enter C: / it displays a complete list of directories and files in C.

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

Update The same thing happens in WPF using C #!

+6
source share
2 answers

C: means "whichever directory is currently selected on the C: drive C: " In your case, this is probably the directory from which your application is running.

D: matches D:/ in your case, because the root directory is the current working directory in D: .

+9
source

This is not a Java question, but a windows / dos question.

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

Entering a drive letter followed by a colon is a command to change the disks in dos, so the `t20 command does nothing, since your working directory is already on drive C. The" directory "returned by the native JRE interface is the same as if you were using path "", that is, your working directory.

Add a slash, on the other hand, and this is the correct path to the root of your C drive, so your JRE gets this directory using its own interface.

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

hope this helps.

+3
source

Source: https://habr.com/ru/post/914255/


All Articles