复制XML文件从RES / XML文件夹来存储设备 [英] Copying Xml File From Res/Xml Folder to Device Storage
问题描述
我试图从RES / XML文件夹中复制一个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.
我知道的出发点是让一个的InputStream
读取XML文件。这是通过使用此实现的:
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);
但我真的很挣扎在如何阅读和从最初的XML文件正确,准确地复制所有信息。
But I'm really struggling on how to read and copy all the information from the initial xml file correctly and accurately.
到目前为止,我一直利用各种的InputStream
和的OutputStream
读写(<$ C $尝试C> DataInputStream以, DataOutputStream类
, OutputStreamWriter
等),但我还是没设法正确地得到它。还有一些未知字符(编码问题?)在产生XML文件。任何人都可以帮助我在这?谢谢!
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!
推荐答案
从 RES / XML
你不能,你必须把所有的文件在你的资产
文件夹,然后使用下面code
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();
}
这篇关于复制XML文件从RES / XML文件夹来存储设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!