| View previous topic :: View next topic |
| Author |
Message |
espeorb
Joined: 03 Nov 2009 Posts: 2
|
Posted: Tue Nov 03, 2009 2:01 pm Post subject: getResourceAsStream problem? |
|
|
Hi!
I'm building a code that will load up a Scanner for an input stream that can be specified whenever a new instance of the class is called. The code is below:
| Code: |
public class refreader {
int currentchar;
String inputfile;
InputStream stream = this.getClass().getResourceAsStream(inputfile);
Scanner refScanner = new Scanner(stream.toString());
public refreader(String inputfile)
{
this.inputfile = inputfile;
}
}
|
Notice it uses getResourceAsStream.
And later on, in my Main java file, I create a new instance of this called 'cr', and it calls to a file in the same direcotry as my other java files called "creatures.txt".
| Code: |
refreader cr = new refreader("creatures.txt");
|
But no matter what I try to do with cr, it always gives me an IOexception, basically saying that it could not find the file specified.
Here's the error log:
| Code: |
init:
deps-clean:
Deleting directory /home/andy/NetBeansProjects/TestApp/build
clean:
init:
deps-jar:
Created dir: /home/andy/NetBeansProjects/TestApp/build/classes
Created dir: /home/andy/NetBeansProjects/TestApp/build/empty
Compiling 4 source files to /home/andy/NetBeansProjects/TestApp/build/classes
/home/andy/NetBeansProjects/TestApp/src/testapp/Main.java:22: unreported exception java.io.IOException; must be caught or declared to be thrown
System.out.println(cr.stream.available());
1 error
/home/andy/NetBeansProjects/TestApp/nbproject/build-impl.xml:365: The following error occurred while executing this line:
/home/andy/NetBeansProjects/TestApp/nbproject/build-impl.xml:170: Compile failed; see the compiler error output for details.
|
But I'm postivitely sure that creatures.txt is right inside of the project's source files! You can see it as an image here:
What am I doing wrong? I was told "getResourceAsStream" loads the resources/files from the folder in which the class calling them is located. Any help would be greatly appreciated! |
|
| Back to top |
|
 |
bhobiger
Joined: 29 Oct 2009 Posts: 52
|
Posted: Tue Nov 03, 2009 2:10 pm Post subject: |
|
|
The error you posted is not a runtime Exception but a compiler error.
Are you sure your program can be built? |
|
| Back to top |
|
 |
espeorb
Joined: 03 Nov 2009 Posts: 2
|
Posted: Tue Nov 03, 2009 2:32 pm Post subject: |
|
|
| bhobiger wrote: | The error you posted is not a runtime Exception but a compiler error.
Are you sure your program can be built? |
I'm sorry, I misphrased that. It's a compiler error that says "unreported exception java.io.IOException; must be caught or declared to be thrown", therefore I said "exception". I'm really new to this but I should have at least caught onto that.
So no, my program can't be built. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|
|
|