如何访问android项目的根目录中的文件,java路径 [英] how to access a file in the root of the android project, java path

查看:287
本文介绍了如何访问android项目的根目录中的文件,java路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的android项目的根目录中有一个配置文件夹,这个项目需要从项目深处的类中访问。目录结构是这样的:
myProject / config / config.properties
myProject / src / com / testProject / util / Configuration.java

I have a configuration folder in the root of my android project and a file in side this project i need to access from a class deep inside the project. the directory structure is like this: myProject/config/config.properties myProject/src/com/testProject/util/Configuration.java

形成Config.java我需要读取config.properties
我尝试使用以下代码:

form the Config.java I need to read config.properties I have tries with the following code:

InputStream stream = Configuration.class.getResourceAsStream("../../../../config/config.properties");

但是流变量为空意味着pat错误。
有人可以通过Config.java文件向我发送配置属性文件的正确相对路径。谢谢

but the stream variable is null meaning the pat is wrong. Can some one please send me the correct relative path to the config properties file searching from the Config.java file. Thanks

推荐答案

从.apk构建手机上的本地文件系统后,很难从项目中访问资源是访问,而不是项目文件。我的解决方案是将我需要访问的文件放在项目/资产文件中,然后可以在getAssets()方法中使用它们。所以路径是这样的:

It is tricky accessing the a resource from the project since after the .apk is build the local file system on the phone is accessed and not the project files. My solution is putting the files that I need to access in the project/assets file and then they can be acesed ussin the getAssets() method. So the path is like this:

myProject/assets/config.properties 
myProject/src/com/testProject/util/Configuration.java

,Configuration.java中的代码是:

and the code inside Configuration.java is:

AssetManager manager = context.getAssets();
stream = manager.open("config/config.properties");

所以重要的是只能访问assets文件夹中的文件,而且还需要一个上下文来调用getAssets()方法。

So the important thing is that only files inside the assets folder can be accessed, and also you need a context to call the getAssets() method.

这篇关于如何访问android项目的根目录中的文件,java路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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