Jump to content

  • Log In with Google      Sign In   
  • Create Account


Reading a File from Another Package


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 destructivArts   Members   -  Reputation: 205

Like
0Likes
Like

Posted 09 May 2012 - 11:02 AM

I'm trying to load level data from a text file for my game, and I've been looking around for a while now for the answer to this problem.
I've gotten this method to work before when the text file was in the same package as the class using it, so I'd rather not have to change the rest of my code to make this work.
I'm using a BufferedReader to try and read a .txt file. I'm reading in a method that throws an IOException.
The method, when it gets to the part where it loads the file, keeps throwing this exception:
IOException: java.io.FileNotFoundException: /Volumes/destructivArts/Programming/Platform%20Game/build/classes/Assets/Levels/level1.txt (No such file or directory)
I followed the path, and thats EXACTLY where the file is.
I'm not sure what's going on.

Here's the important code.
It's in a method called by the constructor of a level class:
public class Level {
    public Level(int i, ImageHandler a){
	    try{
		    readLevel(i);
	    }catch(IOException e){System.out.println("IOException: " + e);};
}
    public void readLevel(int index) throws IOException{
	    URL path = this.getClass().getResource("/Assets/Levels/level"+index+".txt");
	    File levelFile = new File(path.getPath());
BufferedReader reader = new BufferedReader(new FileReader(levelFile));
	   //The rest of the method is just handling the text
    }
}
And here's the structure of my game.
Assets Package
Levels Package (Assets.Levels)
level1.txt
Game Package
Game.java
Level.java

Any help would be greatly appreciated,
Thanks!
Peter
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

Sponsor:

#2 ppgamedev   Members   -  Reputation: 311

Like
2Likes
Like

Posted 11 May 2012 - 03:14 PM

try with this:
	public void readLevel(int index) throws IOException {
	 InputStream stream = this.getClass().getResourceAsStream("/Assets/Levels/level" + index + ".txt");
	 BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
	 //The rest of the method is just handling the text
	}


#3 destructivArts   Members   -  Reputation: 205

Like
0Likes
Like

Posted 11 May 2012 - 07:44 PM

That worked perfectly.
Thank you.
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS