2017-02-14 106 views
1

我有这样的代码:如何修改属性的XML文件(Android Studio中)

private Properties properties; 
    private static final String myFile = "properties.xml"; 

// code for properties 
    File file = new File(getFilesDir(), myFile); 
    properties = new Properties(); 

    try{ 

     if(file.exists()){ 

      FileInputStream fish = openFileInput(myFile); 
      properties.loadFromXML(fis); 
      fis.close(); 
      Toast.makeText(this, "LOADED FROM STORAGE", Toast.LENGTH_SHORT).show(); 
     } else { 

      FileOutputStream fosa = openFileOutput(myFile, Context.MODE_PRIVATE); 
      properties.storeToXML(fos, null); 
      fos.close(); 
      Toast.makeText(this, "CREATED THE FILE", Toast.LENGTH_SHORT).show(); 
     } 

    }catch(Exception e){ 
     Log.wtf("MAIN", "error loading file"); 
    } 

而且我想进入该属性生成xml文件,但它doen't出现在Android工作室项目,有谁知道我能在哪里找到它,或者如何通过代码修改它?

+0

它是在[内部存储(https://commonsware.com/blog/2014/04/07/storage-situation-internal-storage .html)您运行代码的设备或模拟器。 – CommonsWare

回答

0

这里是saveName() & menuActivity()的代码:

public void saveName(View view){ 

    properties.setProperty("name", this.editText.getText().toString()); 

    try{ 
     FileOutputStream fos = openFileOutput(myFile, Context.MODE_PRIVATE); 
     properties.storeToXML(fos, null); 
     fos.close(); 
    }catch(Exception e){ 
     Log.wtf("MAIN", "fail loading file"); 
    } 
    this.textView4.setText(properties.getProperty("name")); 
} 

public void menuActivity(View v){ 
    Intent i = new Intent(this, MenuActivity.class); 
    startActivityForResult(i, MENU_ACTIVITY_REQUEST_CODE); 
}