将 Xml 文件从 Res/Xml 文件夹复制到设备存储

发布于 2024-12-04 04:26:29 字数 627 浏览 0 评论 0原文

我正在尝试将 xml 文件从 res/xml 文件夹复制到设备存储,但我真的很难做到这一点。

我知道起点是获取一个InputStream来读取xml文件。这是通过使用以下方法实现的:

InputStream is = getResources().openRawResource(R.xml.xmlfile);

最终输出流将是:

file = new File("xmlfile.xml");
FileOutputStream fileOutputStream = new FileOutputStream(file);

但我真的很努力如何正确且准确地读取和复制初始 xml 文件中的所有信息。

到目前为止,我已经尝试使用各种 InputStreamOutputStream 来读取和写入(DataInputStreamDataOutputStream、< code>OutputStreamWriter 等),但我仍然无法正确获取它。生成的 xml 文件中存在一些未知字符(编码问题?)。有人能帮我解决这个问题吗?谢谢!

I'm trying to copy an xml file from the res/xml folder to the device storage but I'm really struggling on how to do this.

I know that the starting point is to get an InputStream to read the xml file. This is achieved by using this:

InputStream is = getResources().openRawResource(R.xml.xmlfile);

Eventually the output stream will be:

file = new File("xmlfile.xml");
FileOutputStream fileOutputStream = new FileOutputStream(file);

But I'm really struggling on how to read and copy all the information from the initial xml file correctly and accurately.

So far, I've tried using various InputStream and OutputStream to read and write (DataInputStream, DataOutputStream, OutputStreamWriter, etc.) but I still didn't managed to get it correctly. There are some unknown characters (encoding issue?) in the produced xml file. Can anyone help me on this? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

古镇旧梦 2024-12-11 04:26:29

res/xml 中,您不能将所有文件放入 assets 文件夹中,然后使用以下代码

Resources r = getResources();
AssetManager assetManager = r.getAssets();

File f = new File(Environment.getExternalStorageDirectory(), "dummy.xml");
InputStream is = = assetManager.open("fileinAssestFolder.xml");
OutputStream os = new FileOutputStream(f, true);

final int buffer_size = 1024 * 1024;
try
{
    byte[] bytes = new byte[buffer_size];
    for (;;)
    {
        int count = is.read(bytes, 0, buffer_size);
        if (count == -1)
            break;
        os.write(bytes, 0, count);
    }
    is.close();
    os.close();
}
catch (Exception ex)
{
    ex.printStackTrace();
}

From res/xml you can't you have to put all files in your assets folder then use below code

Resources r = getResources();
AssetManager assetManager = r.getAssets();

File f = new File(Environment.getExternalStorageDirectory(), "dummy.xml");
InputStream is = = assetManager.open("fileinAssestFolder.xml");
OutputStream os = new FileOutputStream(f, true);

final int buffer_size = 1024 * 1024;
try
{
    byte[] bytes = new byte[buffer_size];
    for (;;)
    {
        int count = is.read(bytes, 0, buffer_size);
        if (count == -1)
            break;
        os.write(bytes, 0, count);
    }
    is.close();
    os.close();
}
catch (Exception ex)
{
    ex.printStackTrace();
}
老街孤人 2024-12-11 04:26:29

I think you should use the raw folder instead. Have a look at http://developer.android.com/guide/topics/resources/providing-resources.html.

笑叹一世浮沉 2024-12-11 04:26:29

您还可以使用此代码:

   try {
        InputStream input = getResources().openRawResource(R.raw.XZY);
        OutputStream output = getApplicationContext().openFileOutput("xyz.mp3", Context.MODE_PRIVATE);
        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            output.write(data, 0, count);
        }
        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
    }

当您需要文件时,请使用此代码:

File k =getApplicationContext().getFileStreamPath("xyz.mp3");

You can also use this code:

   try {
        InputStream input = getResources().openRawResource(R.raw.XZY);
        OutputStream output = getApplicationContext().openFileOutput("xyz.mp3", Context.MODE_PRIVATE);
        byte data[] = new byte[1024];
        long total = 0;
        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            output.write(data, 0, count);
        }
        output.flush();
        output.close();
        input.close();
    } catch (Exception e) {
    }

And when you need file use this code:

File k =getApplicationContext().getFileStreamPath("xyz.mp3");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文