Files and resorces...

Hello,


String objFilename = "/test.obj";
objFile = new File(objFilename);
fileReader = new FileReader(objFile);
bufferedReader = new BufferedReader(fileReader);

So with this is working… but the File is in the res folder So e think I have to use something like getClass().getResource(objFilename);

but When I do that tells me “The constructor File(URL) is undefined” … so how can I set this to work?

use InputStreams instead of Files with getResourceAsStream():


InputStream jarInputStream = getClass().getResourceAsStream(jarpath /* relative to class package */);
// I'd actually recommend this:
InputStream jarInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filepath);
fileReader = new FileReader(jarInputStream);

You can also convert a File into an InputStream like this:


InputStream streamFromFile = new FileInputStream(new File(myfilepath));

using this method tells me “The constructor FileReader(InputStream) is undefined”.

You’d need to use the File(URI) constructor:

final URL url = getClass().getResource( objFilename );
final File file = new File( url.toURI() );

Probably easier to get an input-stream though:

final InputStream is = getClass().getResourceAsStream( objFilename );
final BufferedReader r = new BufferedReader( new InputStreamReader( is ) );

I’ve already used or tryed to use the url and it works untill the method its called error: NullPointerException

Actually… its working… thanks for the help guys!

Sorry. Forgot the InputStreamReader step :slight_smile:

(Still would recommend InputStreams instead of URI’s…)