mellissa
mellissa

Reputation: 97

File location for fileinputstream

I'm doing an Android project and I have a .bin format file which I put it right at the root of my Java project (not in any folder). I tried to retrieve it using FileInputStream such as below:

InputStream file = new FileInputStream("filename.bin");

But it couldn't read the file.

Where should I put my bin file? or is there something else that I need to take note of?

[Edit]The error:

Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/ref/FinalReference

[Edit] Solution: Found the solution from here >> Eclipse error: NoClassDefFoundError: java/lang/ref/FinalReference.

1. Find Running configurations -> java application
2. In the new configuration's Classpath tab, find "Android Library" under Bootstrap Entries and remove it.
3. Still in the Classpath tab, select Bootstrap Entries and click the Advanced button.
4. Choose Add Library and click OK.
5. Select JRE System Library and click Next.
6. Select Workspace Default JRE and click Finish.

Upvotes: 2

Views: 12911

Answers (3)

RNJ
RNJ

Reputation: 15552

FileInputStream also takes a file in to one of its constructors.

You can do

File f = new File("filename.bin")
if (f.isFile()) {
    InputStream is = new FileInputStream(f);
} 
else {
    //cope with missing file
}

This way if the file ever disappears or is not where you think it is you will not get a FileNotFoundException

@Srigan makes a good point though to try the / infront

Upvotes: 2

popfalushi
popfalushi

Reputation: 1362

To get current working directory: System.getProperty("user.dir"));

Upvotes: 0

Srijan
Srijan

Reputation: 1274

The above syntax denotes that the file filename.bin resides in the same folder as your code. Try giving an absolute path to the file or put the file in src folder and use

InputStream file = new FileInputStream("/filename.bin");

Upvotes: 0

Related Questions