PepeHands
PepeHands

Reputation: 1406

create and read test file android

For testing my parser in android project I hardcoded some file which I put in application folder in a way like this (my file is 0_0.xml)

enter image description here

now I'm trying to open it with

File inputFile = new File(fileName);

but it says that the file is not found

java.io.FileNotFoundException: /0_0.xml

I did not work much with android but in my just java projects I used to put some files in root directory of a project and this code

Scanner sc = new Scanner(new File(fileName));

worked fine. So how should I open or where should I put my test files to read it in android project?

Upvotes: 0

Views: 66

Answers (2)

Milad Nouri
Milad Nouri

Reputation: 1597

put your file in asset folder (src/main/assets/)

and read file from asset folder:

getAssets().open(filename);

Upvotes: 1

r2DoesInc
r2DoesInc

Reputation: 3771

That filename is the fully qualified file path ON THE ANDROID DEVICE. So its looking for the file at the root of the FS. You need to copy the file to the sdcard or something and use the full path to open it.

Upvotes: 0

Related Questions