使用Android JUnit访问外部文件(在AVD设备外部) [英] Access external files (Outside of AVD device) using Android JUnit

查看:81
本文介绍了使用Android JUnit访问外部文件(在AVD设备外部)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Robotium来自动化Android应用程序.为此创建了一个android测试项目.在项目文件夹中,我想保留一些文件,例如.properties/.xls等,并希望从/write读取这些文件.例如,我在Android测试项目目录( src/main/java/config )下有一个config.properties文件,我想通过编码访问该文件:

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:

对于普通的Java项目,我使用以下代码段加载 config.properties 文件:

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);

相同的代码-作为Android JUnit项目执行时抛出错误,提示 java.io.FileNotFoundException://src/main/java/config/config.properties:打开失败:ENOENT(无此类文件或目录) 并且无法找到文件.

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.

谢谢

西塔姆·贾娜(Sitam Jana) 软件质量检查工程师 Mindfire解决方案

Sitam Jana Software QA Enginner Mindfire Solutions

推荐答案

您不能以这种方式访问​​Java项目的内部结构.在设备上安装apk时,您的Java代码将转换为Dalvik虚拟机的dex代码,并且您的项目中将没有文件结构.

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.

您可以尝试以下操作:

如果您只想读取config.properties,则应使用 http://developer.android.com/reference/android/content/res/AssetManager.html 类,然后将config.properties放在项目的/assets/中.

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.

如果您还想编写.properties文件,请考虑创建这样的临时文件:在Android中创建临时文件.

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

最好的问候.

这篇关于使用Android JUnit访问外部文件(在AVD设备外部)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆