0

In my Java project in netbeans I get a text files contents to populate some internal data structure using this:

InputStream PFile = this.getClass().getResourceAsStream("../../IISP Details/IISP.txt"); // Get File to create IISP's
    String[] PStringArray = new Scanner(PFile, "UTF-8").useDelimiter("\\A").next().split("\\r?\\n");

But when I clean and build my project it isn't working.

Ideas?

Edit:

More info...

The code is called in the cs.analyser.gui.master package in the 'loadMaster' class.

The file is in cs.analyser.IISPDetails.

I can tell that it's not finding the file - are there any alternatives? Or anyways to make the file get bundled with it when I build it?

user3495579
  • 23
  • 1
  • 4
  • 3
    I wouldn't expect `..` to work in `getResourceAsStream` at all. It's used for finding resources that are _on_ the classpath, not "next to" it. – Ian Roberts Apr 03 '14 at 20:47
  • Idea: define what happens *precisely*. Tell how you run your app, where the classes are loaded from, what the class name containing this code is, where the resource is located. And Ian Roberts is right. .. doesn't work in getResourceAsStream(). Use an absolute path starting with `/`, and going from the root of the classpath to your file. – JB Nizet Apr 03 '14 at 20:49
  • @JBNizet sorry can you elaborate more on what the absolute path would look like? – user3495579 Apr 03 '14 at 20:53
  • If the file is in the `cs.analyser.IISPDetails` package then you should use `/cs/analyser/IISPDetails/IISP.txt`. – Bhesh Gurung Apr 03 '14 at 20:55
  • Imagine the file is a class, located in a package, that you want to import. You would use `import com.foo.blah.IISP Details.IISP.txt`. Replace each dot separating packages by a slash, and prepand a slash. The path will become `/com/foo/blah/IISP Details/IISP.txt`. Having white spaces in package names is a very, very bad idea. Don't do that. – JB Nizet Apr 03 '14 at 20:56

1 Answers1

1

Try this

InputStream PFile = this.getClass().getResourceAsStream("IISP.txt"); // Get File to create IISP's
String[] PStringArray = new Scanner(PFile, "UTF-8").useDelimiter("\\A").next().split("\\r?\\n");

and add the IISP.txt to a location that it is in the CLASSPATH.

Don't know about classpath? http://docs.oracle.com/javase/tutorial/essential/environment/paths.html

Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64