将 Xml 文件从 Res/Xml 文件夹复制到设备存储
我正在尝试将 xml 文件从 res/xml 文件夹复制到设备存储,但我真的很难做到这一点。
我知道起点是获取一个InputStream
来读取xml文件。这是通过使用以下方法实现的:
InputStream is = getResources().openRawResource(R.xml.xmlfile);
最终输出流将是:
file = new File("xmlfile.xml");
FileOutputStream fileOutputStream = new FileOutputStream(file);
但我真的很努力如何正确且准确地读取和复制初始 xml 文件中的所有信息。
到目前为止,我已经尝试使用各种 InputStream
和 OutputStream
来读取和写入(DataInputStream
、DataOutputStream
、< 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从
res/xml
中,您不能将所有文件放入assets
文件夹中,然后使用以下代码From
res/xml
you can't you have to put all files in yourassets
folder then use below code我认为你应该使用 raw 文件夹。看看 http://developer.android.com/guide/topics /resources/providing-resources.html。
I think you should use the raw folder instead. Have a look at http://developer.android.com/guide/topics/resources/providing-resources.html.
您还可以使用此代码:
当您需要文件时,请使用此代码:
You can also use this code:
And when you need file use this code: