Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default Problem in reading a file through getResourceAsStream

    public class start {


    private static final String absName = "/test/src/1.txt";

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    InputStream s = start.class.getResourceAsStream(absName);
    System.out.println(s.toString());
    }
    }

    I have a text file "1.txt" and I have put that text file in the folder /test/src/ and want to read it by using the InputStream class, but I am unable to read data from file.

    I know there are other methods but I want to do it with the method getResourceAsStream(); I am getting a java.lang.NullPointerException, which means getResourceAsStream is not working well and it returns NULL.

    I read about relative and absolute path but could not fix it. where should I put the target file. Please advise.

    regards.

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

    Default

    If you are reading the information as a resource, then as far as I understand it, you'll need to use a path that is relative to the location of your class files.

  3. #3
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Just a quick note, what is null actually?


    Java Code:
    		if (s != null) {
    			System.out.println(s.toString());
    		} else {
    			System.out.print("null");
    			
    		}

    s never gets assigned


    http://java.sun.com/j2se/1.5.0/docs/...resources.html
    This example uses "relative resource" names and the mechanism available from the compiler through the -experimental flag, to get a Class object.

    Are you running it with this tag?
    Last edited by mac; 05-31-2010 at 05:44 PM.

  4. #4
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    If you are reading the information as a resource, then as far as I understand it, you'll need to use a path that is relative to the location of your class files.


    I have only one class (start) and both start and 1.txt are at same paths. I even tried with private static final String absName = "1.txt"; but it does not work.

    I copied the file in almost all folders of the project... but no benefit.

    regards

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

  6. #6
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    I did not create any package. I created a simple java application. If it create package by default, I do not know.

    regards

  7. #7
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    though the example , I am following use the package.
    Accessing Resources

    Should I have to create package, if yes, how?

    regards

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

  9. #9
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    :( yes I created package but no benefit.
    package test;

    regards

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

    Default

    So now both your program that uses the resource and the resource itself are contained within this same package?

  11. #11
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    Can you describe ultimately what you are trying to accomplish? For example, once you read it in, then what? Do you need to gain access to the content of the file? What do you want to do with it?

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by novice View Post
    :( yes I created package but no benefit.
    package test;

    regards
    Here's a short explanation. When you want to open a resource as a stream, the resource is searched for relative to the current class, so if the current class is stored in a package, say package/subpackage/YourClass and you are searching for, say, path/to/yourresource, the resource is searched for in the 'location' package/subpackage/path/to. If your resource is pointed to by an absolute path, say, /path/to/yourresource (a leading slash) your resource is searched for starting at the location pointed to by your classpath value.

    kind regards,

    Jos

  13. #13
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    I am using a java application for research work (to do static analysis of that application). Now I want to run that project and very first statement of that application is to read from the file, the information like


    app.0.name: Intelligent Lock Monitor
    app.0.icon: /incubator/il/ui/il_icon.gif
    app.0.class: incubator.il.ui.MutexMonitor
    app.1.name: Pool Monitor
    app.1.icon: /incubator/bof/pool/ui/pool.gif
    app.1.class: incubator.bof.pool.ui.PoolFrame

    but the application failed to read information from that file.

    regards

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by novice View Post
    I am using a java application for research work (to do static analysis of that application). Now I want to run that project and very first statement of that application is to read from the file, the information like


    app.0.name: Intelligent Lock Monitor
    app.0.icon: /incubator/il/ui/il_icon.gif
    app.0.class: incubator.il.ui.MutexMonitor
    app.1.name: Pool Monitor
    app.1.icon: /incubator/bof/pool/ui/pool.gif
    app.1.class: incubator.bof.pool.ui.PoolFrame

    but the application failed to read information from that file.
    What file? I don't understand your example. Have you read my previous short explanation?

    kind regards,

    Jos

  15. #15
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    If your resource is pointed to by an absolute path, say, /path/to/yourresource (a leading slash) your resource is searched for starting at the location pointed to by your classpath value

    I have already did this and the declaration was like "private static final String absName = "/1.txt";"

    Now the class path as mentioned below says path as "src" and the file "1.txt" is already there, but it is not working.






    Contents of ClassPath

    <?xml version="1.0" encoding="UTF-8"?>
    <classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.Standar dVMType/jre6">
    <attributes>
    <attribute name="owner.project.facets" value="jst.java"/>
    </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.contain er"/>
    <classpathentry kind="con" path="eclipse.fproj.jdt.libprov.osgi/jpt.jpa">
    <attributes>
    <attribute name="org.eclipse.jst.component.dependency" value="../"/>
    </attributes>
    </classpathentry>
    <classpathentry kind="output" path="build/classes"/>
    </classpath>


    regards

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by novice View Post
    If your resource is pointed to by an absolute path, say, /path/to/yourresource (a leading slash) your resource is searched for starting at the location pointed to by your classpath value

    I have already did this and the declaration was like "private static final String absName = "/1.txt";"

    Now the class path as mentioned below says path as "src" and the file "1.txt" is already there, but it is not working.
    You're using Eclipse, right? Eclipse normally copies all resources to where the compiled classes are stored. Define another package 'X' and store your file there. You can load it as a stream by searching for it by using /X/yourfile.

    kind regards,

    Jos

  17. #17
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    You're using Eclipse, right? Eclipse normally copies all resources to where the compiled classes are stored.

    yes- I am using Eclipse EE .


    Define another package 'X' and store your file there. You can load it as a stream by searching for it by using /X/yourfile.

    I did this but no benefit.

    I have two pacakges named txt and pkg. The text file "1.txt' is in pkg and the class files to access it are in txt.

    package txt;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintStream;

    class Test {
    private static final String relName = "/pkg/1.txt";
    public static void test1() {

    InputStream s = Test.class.getResourceAsStream(relName);
    System.out.println(s.toString());
    // do something with it.
    }


    }

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

    Default

    Please use code tags. You may want to place both the code and the resource in the same package or better place the resource in a package that branches off of the code package.

  19. #19
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    Please use code tags.
    OK.

    You may want to place both the code and the resource in the same package or better place the resource in a package that branches off of the code package.

    I have done this. same results.

    regards

  20. #20
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by novice View Post
    You're using Eclipse, right? Eclipse normally copies all resources to where the compiled classes are stored.

    yes- I am using Eclipse EE .


    Define another package 'X' and store your file there. You can load it as a stream by searching for it by using /X/yourfile.

    I did this but no benefit.

    I have two pacakges named txt and pkg. The text file "1.txt' is in pkg and the class files to access it are in txt.

    package txt;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintStream;

    class Test {
    private static final String relName = "/pkg/1.txt";
    public static void test1() {

    InputStream s = Test.class.getResourceAsStream(relName);
    System.out.println(s.toString());
    // do something with it.
    }


    }
    I don't know what you're doing or what is possibly wrong. What happens when you run this class (it's in the default package).

    Java Code:
    import java.io.InputStream;
    
    public class T {
    	public static void main(String[] args) {
    		InputStream is= T.class.getResourceAsStream("/T.java");
    		
    		System.out.println(is != null);
    	}
    }
    kind regards,

    Jos

Page 1 of 2 12 LastLast

Similar Threads

  1. Reg: File Reading Problem
    By balaji csc in forum New To Java
    Replies: 0
    Last Post: 11-06-2009, 04:22 PM
  2. Replies: 2
    Last Post: 05-31-2009, 05:03 PM
  3. Weird path problem when reading properties file
    By jerry_popperq in forum New To Java
    Replies: 0
    Last Post: 03-18-2009, 03:32 PM
  4. Problem with reading text from a .txt file
    By Gigi in forum New To Java
    Replies: 40
    Last Post: 01-22-2009, 03:22 AM
  5. Problem reading an xml file with AJAX
    By Fiona80 in forum XML
    Replies: 0
    Last Post: 12-17-2007, 08:02 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
  •