9 Replies Latest reply on Mar 30, 2007 2:55 PM by 807599

    file location

    807599
      I have a program in a package that I know works properly. I had to add a functionality to it that involves reading in a file. That works as well, but only if I do not package the classes and instead put everything in the same directory. I need it to be able to find the file when using the package.

      We are to have a directory structure of coursename/assignment/subdirectory.

      The class accessing the txt file is in the assignment directory, as is the text file itself. We have to be able to compile and run from one level above where the main file is so it has to be compiled from the coursename directory even though the main method is in the assignment directory. I keep getting the error that it can't find the text file and need help trying to figure out how to make the package see the text file.

      The line of code I use to call the file is

      file = new InputFile("examples.txt");

      Any help would be welcomed.

      Message was edited by:
      lostInJava
        • 1. Re: file location
          807599
          Did you try:
          System.getProperty(user.dir) + "assignment/examples.txt"
          Edit: Might need / before "assignment"

          Message was edited by:
          Setter
          • 2. Re: file location
            807599
            No, I did not use that. Do I put it in the class calling the file or the main method?
            • 3. Re: file location
              807599
              Like so:
              String pathToFile = System.getProperty(user.dir) + "assignment/examples.txt";
              file = new InputFile(pathToFile);
              • 4. Re: file location
                807599
                I tried it but am getting the compiler error saying that it can't find the property and pointing to user.dir.
                • 5. Re: file location
                  807599
                  My bad. user.dir should be in quotes:
                  String pathToFile = System.getProperty("user.dir") + "assignment/examples.txt";
                  file = new InputFile(pathToFile);
                  • 6. Re: file location
                    807599
                    I swear I will never understand Java. lol

                    It will compile now (and thank you very much for that), but it won't run. The first error I got was
                    Exception in thread "main" java.io.FileNotFoundException: C:\comp501tme3\tme3_examples.txt (The system cannot find the path specified)
                    at java.io.FileInputStream.open(Native Method)
                    at java.io.FileInputStream.<init>(Unknown Source)
                    at java.io.FileInputStream.<init>(Unknown Source)
                    at java.io.FileReader.<init>(Unknown Source)
                    at tme3.InputFile.readFile(InputFile.java:15)
                    at tme3.InputFile.<init>(InputFile.java:11)
                    at tme3.GreenhouseControls.<init>(GreenhouseControls.java:17)
                    at tme3.GreenhouseController.main(GreenhouseController.java:312)

                    so there needed to be a directory indication between 501 and tme3. I added the / to the beginning of the filename string, but then I get

                    Exception in thread "main" java.io.FileNotFoundException: tme3_examples.txt (The system cannot find the file specified)
                    at java.io.FileInputStream.open(Native Method)
                    at java.io.FileInputStream.<init>(Unknown Source)
                    at java.io.FileInputStream.<init>(Unknown Source)
                    at java.io.FileReader.<init>(Unknown Source)
                    at tme3.InputFile.readFile(InputFile.java:15)
                    at tme3.InputFile.<init>(InputFile.java:11)
                    at tme3.GreenhouseControls.<init>(GreenhouseControls.java:19)
                    at tme3.GreenhouseController.main(GreenhouseController.java:312)
                    • 7. Re: file location
                      807599
                      The path to file seems to be wrong.
                      You need to figure out (exactly)
                      - where your code is running from
                      - where your text file is located relative to that path
                      • 8. Re: file location
                        807599
                        What I don't understand is that the file is indeed in c:\comp501\tme3, so the only problem I saw in the first error was not having the directory indication between 501 and tme3. Adding the / to the front of that should have taken care of that shouldn't it? If I changed the string from "tme3/tme3_examples.txt"; to "/tme3/tme3_examples.txt"; wouldn't that change the reference from comp501tme3 to comp501/tme3?

                        It has to compile and run from the comp501 level but the file and the class using it are both in the tme3 directory.
                        • 9. Re: file location
                          807599
                          It works now. Thank you so much! I don't know why it works. I didn't change the code at all, I just closed the command prompt and reopened it, and it works!