8 Replies Latest reply on Jan 19, 2008 10:52 PM by 807601

    Java file path under windows

    807601
      Hi guys,

      I'm having real trouble getting Java to find my file under Windows. I tried loads of things, but it kept giving me an IOException, so as a basic test, I did :
                java.io.File file = new java.io.File("file:///D:/rss.xml");
                if(file.exists()) {
                     System.out.println("true");
                } else {
                     System.out.println("false");
                }
      However, no matter what combination of strings i try for the File constructor, I can't get it to print true! What am I doing wrong?

      The file's definitely there by the way!!

      Many thanks,
      Nick Moores
        • 1. Re: Java file path under windows
          807601
          Try this:
          new java.io.File("D:" + File.separator + "rss.xml");
          without the "file:///" string
          • 2. Re: Java file path under windows
            807601
            That works - thanks!

            As does "new java.io.File("D:\\rss.xml")" (which you thought i would have tried).

            However, there seems to be a problem with the way i'm trying to do it, as this produces a IOException :
            DOMParser parser = new DOMParser();
            
            parser.parse("D:\\rss.xml");
            I've been passing a string into parser.parse, and normally it's a web URL (http://...), which works fine. Can't seem to get a local file though. Any ideas?

            Many thanks,
            Nick Moores
            • 3. Re: Java file path under windows
              807601
              First you have to create the resources folder in the package of the class you are coding, then copy the XML file to that folder and then try doing this:
              parser.parse(getClass().getResource("resources/rss.xml")); // getResource returns an URL
              I think this should work or maybe:
              parser.parse(getClass().getResource("resources/rss.xml").toURI();
              Let me know if it doesn't and we'll find out which is the problem xD

              cheers!
              • 4. Re: Java file path under windows
                807601
                Hey,

                Sorry, I should perhaps explain why I am doing it this way a bit clearer.

                I have a GUI with a textbox. In the textbox, the user types a URL. A button is pressed, and the parser parses. Therefore, I don't know what file is going to be opened. I just use "Parser.parse(textbox.getText());".

                It works fine with web URLs ("http://..." etc), but can't get it working with local files.

                Since last time, I tried doing it this way :
                java.io.File file = new java.io.File("D:\\rss.xml");
                
                parser.parse(file.getAbsolutePath());
                That actually works. However, 'parser.parse(D:\\rss.xml);' gives an IOException. And file.getAbsolutePath() returns D:\rss.xml. So i'm still pretty stuck...

                Thanks for your reply though! Any other ideas?

                Nick Moores
                • 5. Re: Java file path under windows
                  807601
                  OK,

                  So...

                  I just tried "D:\rss.xml" in the textbox....and it worked! I'm slightly confused, as I was sure i tried that a few...thousand...times before! Ah well, my mistake! It works now, at least!

                  Many thanks,
                  Nick Moores
                  • 6. Re: Java file path under windows
                    807601
                    Lets see if i get it... the user types the url of the file in the text box? If it's so maybe you should consider using a JFileChooser [1] and also you can add a filter to make the file chooser only shows the file types allowed to be parsed.
                    And to get the url of a file you can use
                    File f = new File(path);
                    URL url = f.toURI().toURL();
                    [1] http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html

                    HTH
                    • 7. Re: Java file path under windows
                      807601
                      darkperfect wrote:
                      OK,

                      So...

                      I just tried "D:\rss.xml" in the textbox....and it worked! I'm slightly confused, as I was sure i tried that a few...thousand...times before! Ah well, my mistake! It works now, at least!

                      Many thanks,
                      Nick Moores
                      Hehehe that happens! Don't worry ; )

                      Good luck!
                      • 8. Re: Java file path under windows
                        807601
                        I just tried "D:\rss.xml" in the textbox....and it worked! I'm slightly
                        confused,
                        Backslash escaping (and other escapes) is something that Java source code uses within string literals. It has nothing to do with strings that come from elsewhere (user input, files and so on).

                        So if some code says
                        String str1 = "d:\\foo.txt";
                        And a user types d:\foo.txt, those are exactly the same strings.