This discussion is archived
1 2 Previous Next 15 Replies Latest reply: Aug 21, 2007 8:54 AM by Puce RSS

File's location?

807605 Newbie
Currently Being Moderated
Hi, all professors and experts,

I am using NetBeans 5.5.1 IDE.
I don't know where is a good place to put a file "plotdata.bin".
Thanks
try
    {
      InputStream bis = getClass().getResourceAsStream("plotdata.bin");
      DataInputStream in = new DataInputStream(bis);
Somebody suggests it is in the folder
builder-->
        classes.
bur before I compile it, the folder doen't exist at all.
  • 1. Re: File's location?
    807605 Newbie
    Currently Being Moderated
    Try sobering up!
  • 2. Re: File's location?
    807605 Newbie
    Currently Being Moderated
    What does it mean?
  • 3. Re: File's location?
    807605 Newbie
    Currently Being Moderated
    sober = not drunk.

    Unless of course you were just bullshitting!
  • 4. Re: File's location?
    807605 Newbie
    Currently Being Moderated
    You may misunderstood my meaning.
    My question is I get a null from the resouce.

    I following the tutorial from the sun.com but still get nothing.

    Help me!Thanks

    The getResourceAsStream method finds a resource with a given name. It returns null if it does not find a resource with the specified name. The rules for searching for resources associated with a given class are implemented by the class's ClassLoader. The Class methods delegate to ClassLoader methods, after applying a naming convention: if the resource name starts with "/", it is used as is. Otherwise, the name of the package is prepended, after converting all periods (.) to slashes (/).
    public InputStream getResourceAsStream(String name) {
      name = resolveName(name);
      ClassLoader cl = getClassLoader();
      if (cl==null) {
        return ClassLoader.getSystemResourceAsStream(name); // A system class.
      }
      return cl.getResourceAsStream(name);
    }
    
    public java.net.URL getResource(String name) {
      name = resolveName(name);
      ClassLoader cl = getClassLoader();
      if (cl==null) {
        return ClassLoader.getSystemResource(name);  // A system class.
      }
      return cl.getResource(name);
    }
  • 5. Re: File's location?
    807605 Newbie
    Currently Being Moderated
    Well, a resource isn't always a file, but if it is:

    Use getResource() not getResourceAsStream(). This will give you a URL. Then, if it is a file (and not, for example, a jar entry) getFile() will get you the file path.
  • 6. Re: File's location?
    807605 Newbie
    Currently Being Moderated
    So do I miss something?

    It's wrong
    InputStream bis = new URL(getResource("plotdata.bin").openStream());
  • 7. Re: File's location?
    807605 Newbie
    Currently Being Moderated
    getResource() returns a URL.
  • 8. Re: File's location?
    807605 Newbie
    Currently Being Moderated
    You should consult the API. That last line was just throwing things together:

    http://java.sun.com/javase/6/docs/api/
    Class cls = ... //for example this.getClass() or YourClass.class
    InputStream in = cls.getResourceAsStream(path);
  • 9. Re: File's location?
    Puce Newbie
    Currently Being Moderated
    If you provide the filename only, place it in the same package as you placed your class. Note:
    //This will lookup in the package of the runtime type of this instance, which might be different for a subclass
    InputStream bis = getClass().getResourceAsStream("plotdata.bin");
    
    //This will lookup in the package of MyClass
    InputStream bis = MyClass.class.getResourceAsStream("plotdata.bin");
    For more information see:
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)

    -Puce
  • 10. Re: File's location?
    807605 Newbie
    Currently Being Moderated
        URL u = getResource("plotdata.bin");
          InputStream bis = u.openStream();
          DataInputStream in = new DataInputStream(bis);
    What is wrong?
    **Error: null
  • 11. Re: File's location?
    Puce Newbie
    Currently Being Moderated
    Again: In which directory did you place plotdata.bin? (see my previous post)

    -Puce
  • 12. Re: File's location?
    807605 Newbie
    Currently Being Moderated
    the file is in the project root.
  • 13. Re: File's location?
    Puce Newbie
    Currently Being Moderated
    the file is in the project root.
    ??? Did you read my post at all?

    Use "/plotdata.bin" then. Make sure that it is either in a jar file or that the project root is in your classpath.

    -Puce
  • 14. Re: File's location?
    807605 Newbie
    Currently Being Moderated
    Thanks! It works after I add "/".
1 2 Previous Next