Load in local file

Hi,

I’ve posted here because I feel like Im being a newless clubie at the moment.

The class loading in the file is located at:
com\structures\gui\SteelDesigner.class

The file is located at:
com\structures\resources\UBProperties.csv

So my code is:

try {
      File ubProperties = new File(this.getClass().getResource("../resources/UBProperties.csv").toString());
      fileScanner = new Scanner(ubProperties);
} catch (FileNotFoundException ex) {
    Logger.getLogger(UBTemplateBuilder.class.getName()).log(Level.SEVERE, null, ex);
}

and the output is:
02-May-2010 12:52:29 com.structures.elements.UBTemplateBuilder initTemplates
SEVERE: null
java.io.FileNotFoundException: file:\M:\Java%20Projects\BeamDesigner\build\classes\com\structures\resources\UBProperties.csv (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(FileInputStream.java:106)
at java.util.Scanner.(Scanner.java:636)
at com.structures.elements.UBTemplateBuilder.initTemplates(UBTemplateBuilder.java:34)

Now the directory in the exception is correct but claims it isnt…
I may be missing something stupid here but I’ve put this code together based on abunch of other examples, not any one so if anyone knows the best way to do what Im trying to do I would appriciate it.

By the way Im on Windows XP but of course want OS independancy.

Thanks,

Ken

I’d advise just using the File(String) constructor like so:


File ubProperties = new File("../resources/UBProperties.csv");

But like you mentioned, we want OS independency, so we replace the path separators with the platform one:


File ubProperties = new File(".."+File.separator+"resources"+File.separator+"UBProperties.csv");

I hope that helped :slight_smile:

I cant really help, all I could say is just try another valid method, cause your code looks right to me but Ive never done it quite the same way as you before.

Thanks for the replys, I have used your method of using File.separator which is helpful but this didnt actually solve my problem.

I’ve decided to use a different method pointed out to me by someone because as you say the method seems correct but this new method using input streams seems to have worked.

Thanks anyway.

If you are using Eclipse(and maybe others) you will need to refresh your project if you place new files into the project without using the editor. To do that in Eclipse you select the project folder in the Package Explorer panel and hit F5.

No Im using Netbeans and as far it seems it appears automatically in the projects manager bit. It did just start working and Im still not sure if this new method did actually fix it or if it was something like you mentioned.

Now I have further problems with it, how do I get it to work in a Jar file, it says it cant find it…

You don’t need to use the system’s path separator; in fact, that will mess up Windows paths (because it will give you a ‘’). With Java, paths always use a forward slash ‘/’ and then will be translated under the hood.

It looks like your file path is wrong because you’ve got your resources folder in the wrong place. It should be like this:


MyProjectFolder
    com
        structures
            gui
                SteelDesigner.java
    resources
        UBProperties.csv

Im assuming SteelDesigner’s package is com.structures.gui - if it is, then indeed your directory hierarchy is wrong. Also your source hierarchy is strange. Typically you’d do something like this:

com.mydotcomname.mygamename.mypackagename.MyClass

For example, from my LD17 entry I had:

com.otcsw.zombiegrinder.image.Sprite
com.otcsw.zombiegrinder.util.ResourceManager
etc.

My website is otcsw.com so that makes sense for me. Obviously you can do whatever you want, but your hierarchy is atypical.

So yeah. Move your resources folder to the location I mentioned above, then access it by:


new File(System.getProperty("user.dir") + "/resources/UBProperties.csv")

And that should do the trick.

As far as I know this is not true, unless you’re packaging a JAR. If running from Eclipse it’ll still find all the files correctly (as it’s Java looking for the files, not Eclipse).

Ah thank you so much that is the type of answer I was looking for. No my file system is as follows:

MyProjectFolder
    com
        structures
            gui
                SteelDesigner.java
            resources
                UBProperties.csv

So both folders (gui and resources) are in the structures folder so I presume the following code would be correct then:

new File(System.getProperty("user.dir") + "/structures/resources/UBProperties.csv")

I thought the directory had to be relative to the class refering to it. So the System.getProperty(“user.dir”) will give the directory of my com folder? Even if its in a jar…

Ah yes I see what you mean, I dont work in the industry and Im a lone programmer so I either dont know or forget what good practices are for organising a project. Thanks for the advice its easy to change in netbeans at least, think I might need to rethink things abit…

If you have a jar then presumably all your source is wrapped up in the jar anyway so effectively user.dir will give you the folder that the jar is in. If you’re trying to include the XML file in the jar as well then this is not the correct way to access it (you’ll need to use the jar’s input stream).

But yeah, if your class file is in the com.otsw.whoop folder and it’s also in the com.otcsw.whoop package then the root of your project is user.dir.

Thanks that brilliant.