Results 1 to 20 of 20
  1. #1
    gcampton Guest

    Default file handling problem

    I ran a search for this, but there were not entries I could see, so this is obviously something I'm doing wrong..
    I have a couple files I just added to the src folder in my project, but I keep getting file not found exception. In debug perspective, it brings up a window that says Source not found, and a box Edit Source lookup path.
    Adding my project to the path does not seem to fix the problem.
    Anyone have any ideas?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Are these data files? If so, perhaps the src folder is not where they should be. Do you know exactly where your program is looking for these files? Source code would help here.

  3. #3
    gcampton Guest

    Default

    I'm not used to Eclipse so I'm guessing I'm doing this wrong. Wasn't until after I had created a few classes that I recieved a warning about using "default package"

    Java Code:
    public void validateLogin()
    {
    	try
    	{
    		String name, logName, psswrd, logPsswrd;
    		String inStr;
    		Scanner input = new Scanner(System.in);
    		Scanner inFile = new Scanner(new File("VetSecurity.dat"));
    			
    		System.out.println("Enter name: ");
    		   ...
    	}
    	catch ( FileNotFoundException fnfe)
    	{
    		System.err.printf("%s\n%s\n", getFileName(), "FILE NOT FOUND");
                    System.exit(1);
    	}
    }
    Attached Thumbnails Attached Thumbnails file handling problem-ecli_fnfe.jpg  
    Last edited by gcampton; 12-20-2009 at 08:06 AM.

  4. #4
    gcampton Guest

    Default

    I fixed this by putting full fledged path into filename, But I would still like to know why Eclipse can't see files in the same package.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This has nothing to do with Eclipse. To see where your java is looking to find your files, find your user directory by placing
    Java Code:
    System.out.println(System.getProperty("user.dir"));
    and you can find your files using path names relative to the user dir.

    You can also find your data as resources via methods on your class, and those are relative to the class files, not the src files. Note that these are not files however.

    Corrections welcome.

  6. #6
    gcampton Guest

    Default

    Quote Originally Posted by Fubarable View Post
    This has nothing to do with Eclipse. To see where your java is looking to find your files, find your user directory by placing
    Java Code:
    System.out.println(System.getProperty("user.dir"));
    and you can find your files using path names relative to the user dir.

    You can also find your data as resources via methods on your class, and those are relative to the class files, not the src files. Note that these are not files however.

    Corrections welcome.
    Your right, But I only realized that when I put full fledged path in. This is the way java works with packages.
    So I need to rephrase my question to something else. (I'm unsure what the problem is though, so I cannot rephrase it)

    In JCreator, not working in packages, files in the same directory would be found with java, so the dot(.) path is fine, I'm sure of this.

    using the above:
    Java Code:
    System.out.println(System.getProperty("C:\\Users\\Garratt\\Desktop"));
    on a java file I placed on desktop directory gives back a null value.

    So the question is:
    How do you read files outside of packages but in same directory as java files?
    I tested reading a file using Scanner in jcreator using a project/package and same result, file not found.

    I am on windows btw.
    Last edited by gcampton; 12-20-2009 at 05:02 PM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm wondering if you don't really want the files to be somewhere in relation to the java files, but rather the class files. After all, those are the files that are necessary to run the program. And again, you may wish to look into using resources.

    One trick with Eclipse, if you either drag or drop a file into your package viewer, it is placed in both the src directory with the Java files and in the bin directory with the class files. This also happens if you create a text file in your app (pressing shfit-alt-n then clicking on File). Myself, I usually create a subdirectory / package off of the current one with names such as images for images or data if data...

    But I'm not a pro, so this is just my habit. I"d be interested to hear what the pros do.

  8. #8
    gcampton Guest

    Default

    Ok so I right clicked on package Veterinary. Made a new folder, this seemed to make a new package on the same level as the original:

    Java Code:
    Veterinay (project)
        src (source folder)
            veterinary(package)
                java files....
                ....
    
            data(package)
                data files....
                .....
    Obviously this didn't work. So I cright clicked src folder and clicked new folder, this had the same problem as above, only 1 up level.

    Then I right clicked on src again and choose new "source folder" which made a folder icon with a mini package on it.
    Moving the data files to this tree worked so long as my final file names were as: public static final String SEC_FILE="data\\VetSecurity.dat";
    looks as follows:

    Java Code:
    Veterinary (Project)
        src (source folder)
            veterinary(package)
                java files
                ...
        data(source folder)
            data files
            ...
    is this what you meant? Because from what I can see eclipse doesn't show .class files anywhere. However in the directory tree of my actual documents it's as:

    Java Code:
                                    Veterinary (project folder)
            |                |                |                |
         bin(foder)     data(folder)   src(folder)   tmp(folder)   .classpath   .project
            |                      |            |
         veterinary (folder) .dat files  veterinary(folder)
            |                                   |
        .class files                       .java files
    to me this doesn't make much sense as the data files are now 2 folder levels above the source and binaries. and down 1 level in data folder.
    ( ../../data/xxx.dat )

    yet: data/file.dat
    works...

    I don't understand how this package thing is working. Does java just know that the folder the files are stored in is not actually a folder it's a package. and then the same for the src and bin folders, they seem to not exist either to the jvm. yet, it sees data/ and the files within.

    EDIT: CLASSPATH is:
    Java Code:
    <?xml version.....>
    <classpath>
       <classpathentry kind="src" path="src"/>
       <classpathentry kind="src" path="data"/>
       <classpathentry kind="src" path="tmp"/>
       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.e......"/>
       <classpathentry kind="output" path="bin"/>
    should I change data and tmp to output while eclipse is closed then restart it?
    Last edited by gcampton; 12-21-2009 at 02:10 PM.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Alright, let's see if we can walk you through this.

    A) Go into Eclipse and create a package named "foo"

    B) Now create a subpackage that branches from foo called "data" (actually it will be "foo.data")

    C) Now click on the foo.data package, press shift-alt-n (for new) then click on File and create the file bar.txt in the foo.data package. Type some text into it.

    Eclipse should look something like so:



    D) Now click on the foo package and create a new Java class called Baz that looks something like this:

    Java Code:
    package foo;
    
    import java.io.InputStream;
    import java.util.Scanner;
    
    public class Baz {
      
      private static final String BAR_TEXT = "data/bar.txt";
    
      public void readAndShoeFile() {
        InputStream is = getClass().getResourceAsStream(BAR_TEXT);
        if (is == null) {
          System.out.println("File \"" + BAR_TEXT + "\" not found.  Exiting");
          return;
        }
        Scanner scanner = new Scanner(is);
        
        if (scanner == null) {
          return;
        }
        
        while (scanner.hasNextLine()) {
          System.out.println(scanner.nextLine());
        }
      }
      
      public static void main(String[] args) {
        Baz baz = new Baz();
        baz.readAndShoeFile();
      }
      
    }
    And Eclipse should look like:



    So? Does it work?
    Attached Thumbnails Attached Thumbnails file handling problem-foodata1.jpg   file handling problem-foodata2.jpg  
    Last edited by Fubarable; 12-21-2009 at 03:06 PM.

  10. #10
    gcampton Guest

    Default

    ...wtf INDEEED ? */cry*


    I will create a new project and java files etc, tomorrow just to make sure... But i pretty much followed you examples. Only thing I deviated from was I dragged and dropped my old files into the new package, instead of creating new ones.

    EDIT: I created a new file by right clicking etc, as described above but same error, new filename:
    data\test.txt
    FILE NOT FOUND


    and you were right about it automatically making the same folder structure within the 'bin' folder...
    Attached Thumbnails Attached Thumbnails file handling problem-ecli_fnfe2.jpg  
    Last edited by gcampton; 12-21-2009 at 04:00 PM.

  11. #11
    gcampton Guest

    Default

    EDIT: something skrewy going on. I copy pasted your code and it works fine, but doing the really simple scanner / file reading can't find the file.



    Attached Thumbnails Attached Thumbnails file handling problem-a02.jpg   file handling problem-a01.jpg  
    Last edited by gcampton; 12-23-2009 at 11:35 AM.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    There's nothing screwy at all going on here. Google and read up on the difference between using files and using resources. The JVM will look in different locations: the user.dir for the file and the class directory for the resource.

  13. #13
    gcampton Guest

    Default

    Quote Originally Posted by Fubarable View Post
    There's nothing screwy at all going on here.

    How is your code so different to mine? are we not both creating input file streams and trying to access the same "data/...txt" ?

    Looks to me it IS the same, your just testing the inputstream like a C hacker, and I'm using a simple try/catch

    edit; Or do you mean by "using resource" using "Input stream" as opposed to "file class"?
    Last edited by gcampton; 12-23-2009 at 03:18 PM.

  14. #14
    gcampton Guest

    Default

    bump .... fubarable on holidays :(

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're trying to get files which means that Java is looking starting at the user's current directory. To find out where Java's looking, again, have your program run this line of code:
    Java Code:
    System.out.println(System.getProperty("user.dir"));
    My code on the other hand is getting system resources which means that Java starts looking for my information in a completely different location: the directory where your class files are located.

  16. #16
    gcampton Guest

    Default

    Quote Originally Posted by Fubarable View Post
    There's nothing screwy at all going on here. Google and read up on the difference between using files and using resources. The JVM will look in different locations: the user.dir for the file and the class directory for the resource.
    I am unable to find anything on google or wiki about this....

    I'm also curious where the "pros" place there data files and how they read them.

    My text doesn't talk about resources at all, everything suggested for file reading is inputfilestream(wrapped in Scanner), outputfilestream(wrapped in PrintWriter) and objectfilestream for binaries, and of course file class for testing files/dirs etc.

    "Absolute Java 4th ed" Savitch, International Edition.
    Last edited by gcampton; 12-29-2009 at 11:33 AM.

  17. #17
    gcampton Guest

    Default

    well, yay... i now have an eclipse problem !!!

    I was using your method of getResourceAsStream, and for some reason could not read the files in my project, when I orginally created the files instead of opening in eclipse as they did on my desktop (i'm currently on laptop) they opened up in wordpad... ( i did create the files within eclipse )
    Additionally even though I was getting no filenotfound errors, when I tried to print out what was in the file nothing was happening, so I forced the files to open within eclipse and it told me something about the path to data files were corrupt so I clicked ok, then it opened in eclipse.
    I then went to run and I recieved a classNotFoundException. checked the actual /bin directory and somehow all my .class files have suddenly been deleted and now I can't get eclipse to recomplie...

    funny thing is I did nothing different on my laptop to what I did on the test program "foo" I created on my desktop, same code, same proceedure.

    edit:
    just did a couple more tests and for some reason when creating a ".dat" file it opens automatically in wordpad, but a ".txt" opens in eclipse

    edit2:
    after looking at the directory I closed eclipse and restarted to see if that would solve the problem however it didn't, thene I went away and watched a few episodes of Dr.Who cause I was sick of bashing my head against the wall and didn't particularly want to spend the next 10mins copy pasting all the code from 1 project to another.
    When I came back it seemed to have miraclously compiled itself, works fine now.
    So I guess disregard this post?
    Last edited by gcampton; 12-29-2009 at 02:03 PM.

  18. #18
    gcampton Guest

    Default

    What do you do for writing to files? because I tried creating a generic method to open a OutputStream as resource and keep getting error: Type mismatch cannot convert inputstream to outputstream.

    Java Code:
    public OutputStream openOutFile()
    {
       if (getFileName().length() == 0)
       { 
          System.err. blah blah; System.exit(1);
       }
       
       OutputStream os = getClass().getResourceAsStream(getFileName());
       if ( os == null )
       ...
           ...
        return os;
    }

  19. #19
    gcampton Guest

    Default

    Quote Originally Posted by gcampton View Post
    Quote Originally Posted by Fubarable View Post
    There's nothing screwy at all going on here. Google and read up on the difference between using files and using resources. The JVM will look in different locations: the user.dir for the file and the class directory for the resource.
    I am unable to find anything on google or wiki about this....
    I'm also curious where the "pros" place there data files and how they read them.
    can anyone suggest what search to use? I've tried at least 30 different terms and nothing relevant is coming up.


    edit:
    I found this... http://java.sun.com/javase/6/docs/te...resources.html

    didn't try searching for 1 specific, kept trying to comparison search.
    Last edited by gcampton; 01-05-2010 at 01:12 PM.

  20. #20
    gcampton Guest

    Default

    A system resource is a resource that is either built-in to the system, or kept by the host implementation in, for example, a local file system. Programs access system resources through the ClassLoader methods getSystemResource and getSystemResourceAsStream.

    For example, in a particular implementation, locating a system resource may involve searching the entries in the CLASSPATH. The ClassLoader methods search each directory, ZIP file, or JAR file entry in the CLASSPATH for the resource file, and, if found, returns either an InputStream, or the resource name. If not found, the methods return null. A resource may be found in a different entry in the CLASSPATH than the location where the class file was loaded.
    So does that mean that the "data/*" is irrelevant as the file could be 10 subfolders deep but so long as we had
    getResourceAsStream("filename.txt") it would still find it...?

    this is relative resource as opposed to absolute resource "/pkg/folder/folder/file.file"
    Last edited by gcampton; 01-05-2010 at 01:28 PM.

Similar Threads

  1. handling WAV file
    By zellazode in forum New To Java
    Replies: 1
    Last Post: 09-08-2009, 03:46 PM
  2. Sms handling in j2ee
    By prts_mfs in forum Java Servlet
    Replies: 1
    Last Post: 04-22-2009, 08:40 AM
  3. Problem with JSP exception handling page
    By sidster in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-19-2008, 07:28 PM
  4. Problem in File Handling in Java
    By getbiplab in forum New To Java
    Replies: 6
    Last Post: 03-31-2008, 12:02 PM
  5. problem with event handling!!!
    By ahdus in forum Java Applets
    Replies: 1
    Last Post: 11-17-2007, 06:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •