Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用android Studio创建的android项目中,将自己的属性文件放在何处?_Android_Android Studio - Fatal编程技术网

在使用android Studio创建的android项目中,将自己的属性文件放在何处?

在使用android Studio创建的android项目中,将自己的属性文件放在何处?,android,android-studio,Android,Android Studio,我刚开始使用Android Studio。我不知道把我自己的属性文件放在哪里,因为项目结构中没有资产文件夹 发布的代码片段在eclipse中运行良好,但在AndroidStudio中则不行 代码: Properties prop = new Properties(); try { //load a properties file prop.load(new FileInputStream("app.properties"));

我刚开始使用Android Studio。我不知道把我自己的属性文件放在哪里,因为项目结构中没有资产文件夹

发布的代码片段在eclipse中运行良好,但在AndroidStudio中则不行

代码:

    Properties prop = new Properties();

    try {
        //load a properties file
        prop.load(new FileInputStream("app.properties"));

        //get the property value and print it out
        System.out.println(prop.getProperty("server_address"));

    } catch (IOException ex) {
        ex.printStackTrace();
    }
问题1: 在Android Studio(v0.5.8)中将我自己的属性文件放在哪里

问题2:
如何访问它们?

默认情况下,资产文件夹位于
src/main/assets
中,如果它不存在,请创建它

然后,您可以使用以下方式访问该文件:

getBaseContext().getAssets().open("app.properties")

您可以找到有关Gradle Android项目结构的更多信息。

在主文件夹中创建一个子文件夹,并将其命名为资产。将所有.properties文件放在此(资产)文件夹中

src->main->assets->mydetails.properties

您可以使用AssetManager类访问它

public class MainActivity extends ActionBarActivity {

TextView textView;
private PropertyReader propertyReader;
private Context context;
private Properties properties;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this;
    propertyReader = new PropertyReader(context);
    properties = propertyReader.getMyProperties("mydetails.properties");
    textView = (TextView)findViewById(R.id.text);
    textView.setText(properties.getProperty("Name"));
    Toast.makeText(context, properties.getProperty("Designation"), Toast.LENGTH_LONG).show();
    }
}


public class PropertyReader {

private Context context;
private Properties properties;

public PropertyReader(Context context){
    this.context=context;
    properties = new Properties();
}

public Properties getMyProperties(String file){
    try{
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = assetManager.open(file);
        properties.load(inputStream);

    }catch (Exception e){
        System.out.print(e.getMessage());
    }

    return properties;
    }
}

您可以在主文件夹中创建资产子文件夹,然后在其中插入.properties文件

然后,创建一个类来打开并读取文件,例如:

public class PropertiesReader { 

 private Context context;
 private Properties properties;
 public PropertiesReader(Context context) { 
  this.context = context;
   //creates a new object ‘Properties’
   properties = new Properties();

   public Properties getProperties(String FileName) {
   try {    
    //access to the folder ‘assets’ 
    AssetManager am = context.getAssets(); 
    //opening the file    
    InputStream inputStream = am.open(FileName); 
    //loading of the properties 
    properties.load(inputStream);
   } 
   catch (IOException e) { 
    Log.e("PropertiesReader",e.toString());
   }
  }
  return properties;
 } 
}
有关详细信息,请参阅

文件夹结构:


我认为默认情况下,
assets
文件夹位于
src/main/resources/assets
中。如果文件夹不存在,尝试创建它并使用
context.getAssets().open(“app.properties”)
Salem,请将
context.getAssets().open(“app.properties”)
更改为
getBaseContext().getAssets().open(“app.properties”)
(更准确地说)并将您的评论作为答案发布,重点是您的!对不起,我给你的信息是错误的。请检查我的编辑。好的,没问题。如果用户想稍后更新其属性并将其保存到非易失性存储中,那么从资产加载是个坏主意,因为这是只读的。最好在其他地方初始化属性。tuto很棒,比X好多了!
public static String getProperty(String key, Context context) throws IOException {
    try {
    Properties properties = new Properties();
    AssetManager assetManager = context.getAssets();
    InputStream inputStream = assetManager.open("config.properties");
    properties.load(inputStream);
    return properties.getProperty(key);
   }catch (IOException e){
   e.fillInStackTrace();
   }
return null;
}