Results 1 to 20 of 27
- 05-31-2010, 05:05 PM #1
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
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.
-
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.
- 05-31-2010, 05:28 PM #3
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.
- 05-31-2010, 05:44 PM #4
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
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
-
This may not help at all, but I'm wondering: Is your code in a package?
- 05-31-2010, 05:56 PM #6
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
I did not create any package. I created a simple java application. If it create package by default, I do not know.
regards
- 05-31-2010, 05:59 PM #7
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
though the example , I am following use the package.
Accessing Resources
Should I have to create package, if yes, how?
regards
-
Again, I have no idea if this will help you in your quest, but suspect that it might. So I recommend that all your code be in packages. For more on packages, please check out the Sun tutorial on this.
What Is a Package? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)
Creating and Using Packages (The Java™ Tutorials > Learning the Java Language > Packages)
- 05-31-2010, 06:05 PM #9
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
:( yes I created package but no benefit.
package test;
regards
-
So now both your program that uses the resource and the resource itself are contained within this same package?
- 05-31-2010, 06:09 PM #11
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?
- 05-31-2010, 06:12 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
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
- 05-31-2010, 06:17 PM #13
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
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
- 05-31-2010, 06:25 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 05-31-2010, 06:29 PM #15
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
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
- 05-31-2010, 06:55 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
- 05-31-2010, 07:07 PM #17
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
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.
}
}
-
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.
- 05-31-2010, 07:33 PM #19
Member
- Join Date
- May 2010
- Posts
- 30
- Rep Power
- 0
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
- 05-31-2010, 08:11 PM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
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).
kind regards,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); } }
Jos
Similar Threads
-
Reg: File Reading Problem
By balaji csc in forum New To JavaReplies: 0Last Post: 11-06-2009, 04:22 PM -
[SOLVED] java.lang.NullPointerException when reading getClass().getResourceAsStream(.
By jon80 in forum New To JavaReplies: 2Last Post: 05-31-2009, 05:03 PM -
Weird path problem when reading properties file
By jerry_popperq in forum New To JavaReplies: 0Last Post: 03-18-2009, 03:32 PM -
Problem with reading text from a .txt file
By Gigi in forum New To JavaReplies: 40Last Post: 01-22-2009, 03:22 AM -
Problem reading an xml file with AJAX
By Fiona80 in forum XMLReplies: 0Last Post: 12-17-2007, 08:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks