12 Replies Latest reply: May 22, 2009 2:46 AM by 794254 RSS

    Java file path problem

    807588
      In Windows, I confused that File file = new File("c:"); is different from File file = new File("c:\\");

      Anybody can explain it for me?
      Thanks.
        • 1. Re: Java file path problem
          794254
          Are you saying that they're different? If so, how?

          If you're asking if they're different, then no, they're not.
          • 2. Re: Java file path problem
            camickr
            Open up a Dos window and go to the directory where you have your Java source files. Then try:

            1) cd c:
            2) cd c:\

            One leaves you at the current directory. The other takes you to the root of the c drive.
            • 3. Re: Java file path problem
              794254
              camickr wrote:
              Open up a Dos window and go to the directory where you have your Java source files. Then try:

              1) cd c:
              2) cd c:\

              One leaves you at the current directory. The other takes you to the root of the c drive.
              But isn't that cd specific behaviour? The help for the command says:
              Type CD drive: to display the current directory in the specified drive.
              Type CD without parameters to display the current drive and directory.
              I'm not sure how this relates to Java file handling. I don't see any difference between c:, c:\\ or *c:/" as far as creating a new File is concerned.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
              • 4. Re: Java file path problem
                camickr
                I'm not sure how this relates to Java file handling
                import java.util.*;
                import java.io.*;
                
                public class Test
                {
                     public static void main(String args[])
                     {
                          File dir1 = new File("c:");
                          File dir2 = new File("c:\\");
                          System.out.println(Arrays.asList(dir1.listFiles()));
                          System.out.println(Arrays.asList(dir2.listFiles()));
                     }
                }
                • 5. Re: Java file path problem
                  794254
                  camickr wrote:
                  I'm not sure how this relates to Java file handling
                  import java.util.*;
                  import java.io.*;
                  
                  public class Test
                  {
                       public static void main(String args[])
                       {
                            File dir1 = new File("c:");
                            File dir2 = new File("c:\\");
                            System.out.println(Arrays.asList(dir1.listFiles()));
                            System.out.println(Arrays.asList(dir2.listFiles()));
                       }
                  }
                  I'd tried exactly this before I replied to the OP. Sorry, but I don't see any difference between the two.
                  • 6. Re: Java file path problem
                    EJP
                    I don't see any difference between c:, c:\\ or *c:/" as far as creating a new File is concerned.
                    C: would create it in the current directory. C:\\ or C:/ would create it in the root directory. If the current directory is the root directory, there is no difference. If it isn't, there is.

                    This is MS.DOS 101 actually. Nothing to do with Java of course.
                    • 7. Re: Java file path problem
                      794254
                      ejp wrote:
                      I don't see any difference between c:, c:\\ or *c:/" as far as creating a new File is concerned.
                      C: would create it in the current directory. C:\\ or C:/ would create it in the root directory. If the current directory is the root directory, there is no difference. If it isn't, there is.

                      This is MS.DOS 101 actually. Nothing to do with Java of course.
                      Ok, I must be particularly dense because I still don't get it. Both c: and c:// (or c:\) resolve to the absolute path "c:\" for me (when calling getAbsolutePath() for their respective File objects).
                      C: would create it in the current directory. C:\\ or C:/ would create it in the root directory. If the current directory is the root directory, there is no difference. If it isn't, there is.
                      By creating a new File, I meant new File("c:") or new File("c:\\"), not sure if that's what you understood it to be. Are you perchance referring to making a new file from the console?

                      Edited by: nogoodatcoding on May 22, 2009 12:23 PM
                      • 8. Re: Java file path problem
                        EJP
                        Ok, I must be particularly dense because I still don't get it. Both c: and c:// (or c:\) resolve to the absolute path "c:\" for me
                        If your current directory is the root.

                        Put it this way. C: means '.' on the C: drive, and they are both equivalent to C:\ if '.' is \. Not otherwise.

                        Try this from the console:

                        cd C:\temp
                        cd
                        cd c:
                        cd

                        According to you this should print C:\temp and then C:\. It won't. It will print C:\temp both times. Same applies when creating files in Java.

                        OK?
                        By creating a new File, I meant new File("c:") or new File("c:\\")
                        Me too.
                        • 9. Re: Java file path problem
                          794254
                          Ok, I have to admit that I didn't actually understand what you were trying to point out, so I went and talked to someone here. And now I get it. The reason I didn't earlier was that I didn't know that you could use "drive:myfoldername" to access myfoldername at the current directory of drive:, this isn't a syntax I'm familiar with.

                          So, to clarify for anyone else who's paying attention, try this:
                               public class TestFilePath 
                               {
                                    public static void main(String args[]) throws IOException
                                    {
                                              File dir1 = new File("c:temp");
                                              File dir2 = new File("c:\\temp");
                          
                                              System.out.println(dir1.getAbsolutePath());
                                              System.out.println(dir2.getAbsolutePath());
                                         }
                               }
                          1. Save and compile this in c:\temp
                          2. Open up a terminal and navigate to c:\temp and run java TestFilePath
                          3. The output will be
                          *c:\temp\temp
                          c:\temp*

                          Because c: resolves to the current directory at the console which happens to be c:\temp. Then, the rest of the path is appended which makes it c:\temp\temp
                          c:\\temp works as you'd expect it to.

                          4. If you move the class to c:\ and try java TestFilePath there, you'll get
                          *c:\\temp
                          c:\temp*
                          as the output

                          Thanks ejp

                          Edited by: nogoodatcoding on May 22, 2009 1:14 PM

                          Edited by: nogoodatcoding on May 22, 2009 1:15 PM
                          • 10. Re: Java file path problem
                            camickr
                            I'd tried exactly this before I replied to the OP. Sorry, but I don't see any difference between the two.
                            As I said in my original posting - "go to the directory where you keep your Java source files". I hope you don't keep all you file in the root of the C: drive. You will see an obvious difference.
                            • 11. Re: Java file path problem
                              DrClap
                              Also, if I'm not mistaken, Windows has the concept of "current drive" and each drive has its own current directory. So your current working directory on a Windows box is the current directory of the current drive. I don't believe that other operating systems do that (but I could be wrong).
                              • 12. Re: Java file path problem
                                794254
                                camickr wrote:
                                I'd tried exactly this before I replied to the OP. Sorry, but I don't see any difference between the two.
                                As I said in my original posting - "go to the directory where you keep your Java source files".
                                Sorry, I must've skimmed over that.
                                I hope you don't keep all you file in the root of the C: drive.
                                I should hope not! :)
                                You will see an obvious difference.
                                I do now!