13 Replies Latest reply: Oct 17, 2009 11:47 AM by 807580 RSS

    windows 7 and file input

    807580
      With the following code, it throws an error while trying to instantiate the scanner because apparently the file doesn't exist. I've tried putting the file in multiple places and using both absolute and relative paths. I am assuming the issue is with Windows 7. Any ideas?
              File file = null;
              Scanner scan = null;
              try{
                  //JOptionPane.showMessageDialog(null,File.exists("C:\\Users\\stephen\\Documents\\javaprojects\\file.txt")); //also tried it with / and just file.txt
                  file = new File("file.txt");
                  
                  JOptionPane.showMessageDialog(null,file.exists());
                  scan = new Scanner(file);
              }
              catch (Exception ex){JOptionPane.showMessageDialog(null,"Error while opening file"); System.exit(0);}
        • 1. Re: windows 7 and file input
          807580
          stephensk8s wrote:
          I am assuming the issue is with Windows 7.
          Given what evidence?

          Have you put the file in the user.dir?
          • 2. Re: windows 7 and file input
            807580
            Given the evidence that I just installed Windows 7 (obviously). I'm also not a beginner so I should know what I'm doing, but I guess I don't haha. I did some file I/O on Vista recently and it worked fine. I don't believe I used a Scanner, but it shouldn't matter, because it's not finding the file. exists is returning false.

            If by user.dir you mean the directory that the program is in, then yes, I have it in there and have tried to access it from there.
            • 3. Re: windows 7 and file input
              807580
              stephensk8s wrote:
              Given the evidence that I just installed Windows 7 (obviously). I'm also not a beginner so I should know what I'm doing, but I guess I don't haha. I did some file I/O on Vista recently and it worked fine. I don't believe I used a Scanner, but it shouldn't matter, because it's not finding the file. exists is returning false.

              If by user.dir you mean the directory that the program is in, then yes, I have it in there and have tried to access it from there.
              People still use windows??
              • 4. Re: windows 7 and file input
                807580
                I'm a bit lazy. More than a bit. One day I'll get down to installing linux on here. Until then, I'm missing out. But Windows 7 is actually pretty nice.
                • 5. Re: windows 7 and file input
                  807580
                  stephensk8s wrote:
                  If by user.dir you mean the directory that the program is in, then yes, I have it in there and have tried to access it from there.
                  No, I mean user.dir which can be obtained from System properties. Based on your answer to my question, I am willing to bet you any amount that the error is neither Java's nor Windows 7.

                  edit: if you place this in your code:
                  System.out.println(System.getProperty("user.dir"));
                  and place the file in this directory, will your program now work?

                  Edited by: Encephalopathic on Oct 16, 2009 5:57 PM
                  • 6. Re: windows 7 and file input
                    807580
                    I just checked what the user.dir was with System.getProperty and it returned where I had the file and where the program was located so I don't think that's the issue.


                    I appreciate the help by the way.

                    Edited by: stephensk8s on Oct 16, 2009 6:07 PM
                    • 7. Re: windows 7 and file input
                      807580
                      Not Windows 7 based, I ahve the 64bit RC and everything works as normal. (Except Toolkit.beep(), for some reason :o)
                      • 8. Re: windows 7 and file input
                        807580
                        Jadz_Core wrote:
                        Not Windows 7 based, I ahve the 64bit RC and everything works as normal. (Except Toolkit.beep(), for some reason :o)
                        I have the full version of 7 64 bit, but that shouldn't matter very much.
                        Does my code run on your computer?


                        Toolkit.beep seems to work on my system.

                        Edited by: stephensk8s on Oct 16, 2009 10:38 PM
                        • 9. Re: windows 7 and file input
                          807580
                          If you declare a file with just a name, it will look for a file in the current working directory of the program. Usually the CWD if you're running from an IDE is not the home directory, but some other place like the IDE project's build or bin directory. Use the full path to the file name.
                          • 10. Re: windows 7 and file input
                            807580
                            I actually put it in the IDE project's bin directory too. When I checked the CWD it was where I thought it was though. And when I tried the full pathname that failed too.
                            • 11. Re: windows 7 and file input
                              807580
                              It looks like no one else answering this thread has Windows 7 unfortunately. Just to debug in the other direction, have you tried your code on a non-Windows 7 machine?

                              Have you tried creating a very simple program that writes "hello world" to a file, then tries to read back from the file and print the text to the screen? I think your problem calls for more debugging. Please let us know what happens.
                              • 12. Re: windows 7 and file input
                                807580
                                I tried to send it to a non-Windows 7 machine but the person was going to be away from a computer for a long time. I'll try what you suggested and report back.
                                In the meantime, can anyone in this thread try it on their computer?



                                EDIT:
                                Wooow. Thanks for having me do that encephalopathic, and thanks for everyone else's help. Apparently explorer is set to not show me the extensions of files. So the name was actually file.txt.txt. It works now.

                                Edited by: stephensk8s on Oct 17, 2009 9:25 AM
                                • 13. Re: windows 7 and file input
                                  807580
                                  stephensk8s wrote:
                                  Wooow. Thanks for having me do that encephalopathic, and thanks for everyone else's help. Apparently explorer is set to not show me the extensions of files. So the name was actually file.txt.txt. It works now.
                                  Aha! Glad you solved it and had some fun debugging to boot. :)