Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

java - Access external files (Outside of AVD device) using Android JUnit

I am using Robotium to automate Android application. Created a android test project for the same. Inside project folder I want to keep some files like .properties/.xls etc. and want to read from /write to those files. For example, I have one config.properties file under my Android test project directory (src/main/java/config) and I want to access that file through coding:

For normal Java project I used following code snippet to load config.properties file:

CONFIG = new Properties();
FileInputStream fs = new FileInputStream(System.getProperty("user.dir")+"/src/main/java/config/config.properties");
CONFIG.load(fs);

This same code - when executed as Android JUnit project throwing error saying java.io.FileNotFoundException: //src/main/java/config/config.properties: open failed: ENOENT (No such file or directory) and unable to locate the file.

If anyone faced this anytime before please help me to get through.

Thanks

Sitam Jana Software QA Enginner Mindfire Solutions

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot access the internal structure of your java project that way. When you install your apk on the device, your java code gets converted to dex code for the Dalvik Virtual Machine, and you won't have the file structure from your project.

You can try the following:

If you only want to read the config.properties you should use the http://developer.android.com/reference/android/content/res/AssetManager.html class, and put your config.properties in /assets/ in your project.

If you also want to write your .properties file, consider creating a temporary file like this: Creating temporary files in Android.

Best Regards.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...