gpt4 book ai didi

java - 如果文件在 Assets 目录下,如何将文件名作为字符串获取?

转载 作者:太空狗 更新时间:2023-10-29 12:50:21 26 4
gpt4 key购买 nike

  • 我在 Assets 目录 (com.android.project\assets\xml\file.xml) 下有 xml 文件。
  • 我想调用一个(以下)函数来读取 xml 文件并将内容作为字符串返回。
  • 该函数需要文件的路径作为字符串。我不知道如何将文件的路径作为字符串给出。

    private String getXML(String path){

    String xmlString = null;

    AssetManager am = this.getAssets();
    try {
    InputStream is = am.open(path);
    int length = is.available();
    byte[] data = new byte[length];
    is.read(data);
    xmlString = new String(data);
    } catch (IOException e1) {
    e1.printStackTrace();
    }

    return xmlString;
    }

文件.xml:

    <Items>
<ItemData>
<ItemNumber>ABC</ItemNumber>
<Description>Desc1</Description>
<Price>9.95</Price>
<Weight>10.00</Weight>
</ItemData>
<ItemData>
<ItemNumber>XYZ</ItemNumber>
<Description>"Desc2"</Description>
<Price>19.95</Price>
<Weight>22.22</Weight>
</ItemData>
</Items>

问题:

  • 如果我的文件位于\assets\xml\file.xml 下,我如何调用函数 getXML(String path) 并将路径作为字符串参数?

  • 最后,有没有更好的方法将 XML 文件作为字符串读取?

谢谢!

最佳答案

以下是可行的:

InputStream is = context.getAssets().open("xml/file.xml");

关于java - 如果文件在 Assets 目录下,如何将文件名作为字符串获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12722532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com