Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Load in local file  (Read 1263 times)
0 Members and 1 Guest are viewing this topic.
Offline Marksman Ken

Senior Newbie





« Posted 2010-05-02 12:04:16 »

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:
1  
2  
3  
4  
5  
6  
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.<init>(FileInputStream.java:106)
        at java.util.Scanner.<init>(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
Offline jimsmi6

Senior Newbie





« Reply #1 - Posted 2010-05-02 14:51:25 »

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

1  
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:


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


I hope that helped Smiley
Offline rouncer

Junior Member





« Reply #2 - Posted 2010-05-02 15:44:52 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Marksman Ken

Senior Newbie





« Reply #3 - Posted 2010-05-02 16:48:22 »

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.
Offline zoto

Senior Member


Medals: 4



« Reply #4 - Posted 2010-05-03 03:11:51 »

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.
Offline Marksman Ken

Senior Newbie





« Reply #5 - Posted 2010-05-05 15:42:51 »

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...
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #6 - Posted 2010-05-05 16:24:22 »

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:

1  
2  
3  
4  
5  
6  
7  
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:
1  
new File(System.getProperty("user.dir") + "/resources/UBProperties.csv")


And that should do the trick.

See my work:
OTC Software
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #7 - Posted 2010-05-05 16:39:48 »

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.
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).

See my work:
OTC Software
Offline Marksman Ken

Senior Newbie





« Reply #8 - Posted 2010-05-06 10:25:15 »

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

1  
2  
3  
4  
5  
6  
7  
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:

1  
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...
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #9 - Posted 2010-05-06 13:21:51 »

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.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Marksman Ken

Senior Newbie





« Reply #10 - Posted 2010-05-07 09:49:59 »

Thanks that brilliant.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (11 views)
2014-08-28 18:26:30

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (37 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (29 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!