简体   繁体   English

Android:如何为数据容器创建基于 XML 的文件,从文件中读取/添加内容

[英]Android: How to create a XML - based file for the data container, read from file/add content

In the context of a project, i would like to create a data container based on XML-Files.在一个项目的上下文中,我想创建一个基于 XML 文件的数据容器。

Every entry should be a XML-File.每个条目都应该是一个 XML 文件。

For this task i created a new directory which looks as the following: DataBase对于这个任务,我创建了一个新目录,如下所示: DataBase

In order to fill the DataBase with XML-Files the following should be done:为了用 XML 文件填充数据库,应执行以下操作:

  1. Create a XML-File in the DataBase Folder.在数据库文件夹中创建一个 XML 文件。
  2. Add content to said XML-File.将内容添加到所述 XML 文件。
  3. Eventually change things from the XML-File.最终改变 XML 文件的内容。

What i tried:我尝试了什么:

For 1:对于 1:

int exampleNumber= 1444;
FileOutputStream example = openFileOutput("app/src/main/java/DataBase/FileName.xml", MODE_PRIVATE); 

example.write(exampleNumber);
example.flush();
example.close();

Issue: Doesn't do anything.问题:什么都不做。

For 2:对于 2:

private String PATH = "app/src/main/java/DataBase/FileName.xml";

public void addToXML(String Text) throws IOException {
String textToAppend = Text;
BufferedWriter writer = new BufferedWriter(new FileWriter(this.PATH, true));
writer.write(textToAppend);
writer.close();
}

Issue: For the 2. to work 1. must work.问题:为了 2. 工作 1. 必须工作。 1->2 1->2

For 3:对于 3:

Same Issue as above与上述相同的问题

Here is a simple code to demonstrate my words.这是一个简单的代码来演示我的话。 (i have tested it on my Note 9) (我已经在我的 Note 9 上测试过)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    // Keep your preference in memory
    SharedPreferences pref = getApplicationContext().getSharedPreferences("my_xml_file", 0); // 0 - for private mode
    SharedPreferences.Editor editor = pref.edit();
    
    // Change values
    editor.putBoolean("a_boolean_value", true);
    editor.putInt("an_int_value", 42);
    
    // Apply modification to the file
    editor.commit();
    editor.apply();
    
    try { // Read the file as a string and log it
        Log.i(getClass().getSimpleName(), readAsString("my_xml_file.xml"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public String readAsString(String file) throws IOException {
    FileInputStream fileInputStream = new FileInputStream(new File(new File(getDataDir(), "shared_prefs"), file));
    // Preference files are stored in: /data/data/<your package>/shared_prefs/<name>.xml
    
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream, StandardCharsets.UTF_8))) {
        StringBuilder builder = new StringBuilder();
        
        String line;
        while ((line = reader.readLine()) != null) {
            builder.append(line);
            builder.append('\n');
        }
        
        return builder.toString();
    }
}

Hope it will help you.希望它会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM