Android uses a stream to copy XML files in the project to sdcard (including garbled characters)-it took a long time

Source: Internet
Author: User

Recently I wrote a project. when running the program, I need to copy the content of an XML file in the program to sdcard, in this process, I encountered a very tangled problem-the garbled problem. When I saw dozens of methods and was about to give up, I succeeded... The result is so unexpected. Let's take a look at the process.

1. first determine whether the sdcard is mounted, and then create the required folder and file on the card. The Code is as follows:

File sdcarddir = environment. getexternalstoragedirectory (); If (environment. media_mounted.equals (environment. getexternalstoragestate () {// get a path with the folder path and name string path of sdcard = sdcarddir. getpath () + "/profitdata"; // obtain the/sdcard/profitdata path file folder = new file (PATH); // create a folder file data = new file (path, "data. XML "); // create an XML file if (! Folder. exists () {// if not, create a directory. You can create folder. mkdirs () ;}if (! Data. exists () {data. createnewfile ();}

2. Get the XML file from the project using the stream method. The method is as follows:
Inputstream input = getresources (). openrawresource (R. xml. data); // place the file under Res/XML
Inputstreamreader inputreader = new inputstreamreader (input, "UTF-8 ");
Breader = new bufferedreader (inputreader); // wrap it into reader
Create an output stream to import data to the card. The method is as follows:
FW = new filewriter (data); // data is created in method 1
Then start Data Transmission
While (line = breader. Readline ())! = NULL ){
FW. Write (line );
}
However, this problem occurs. The XML data read from the project is garbled. I checked a lot of methods and struggled for a long time. I tried coding and everything, not easy to use .. Finally, we found a method:

I found an article saying this: Normally, assets files are created by default during project creation. Of course, we can also create raw folders under res files, which can store some images, audio or text information can be used in the program, but there are also differences between them. Files under assets will not be compiled, and content can be accessed through paths. Raw files are automatically compiled. We can find the corresponding ID in the R. Java file. If the file is relatively large, it will be placed in the aeests file, because using the information in this file is equivalent to performing Io stream operations, relatively time-consuming operations.
It is important to obtain the resource methods in the assets and raw folders:
Assets: assetmanager = getassets ();
Raw: inputstream = getresources (). openrawresource (R. Raw. Demo );

I tried it and the result was successful.
I copied the XML file to assets, and changed the corresponding input stream to assetmanager. I was very excited.
The method is to replace the input in method 2 with the following input:

Assetmanager = getassets ();
Inputstream input = NULL;
Try {
Input = assetmanager. Open ("data. xml ");
} Catch (ioexception e ){
E. printstacktrace ();
}
Inputstreamreader inputreader = new inputstreamreader (input, "UTF-8 ");
Breader = new bufferedreader (inputreader );
FW = new filewriter (data );
String line = "";
While (line = breader. Readline ())! = NULL ){
FW. Write (line );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.