Read File garbled

Source: Internet
Author: User
// The getresource method uses UTF-8 to encode the path information. When there are Chinese characters and spaces in the path, it will convert these characters, the obtained path is often not the actual path we want. Here, we call the decode method of urldecoder to decode the URL to obtain the original Chinese and space paths. String configpath = xmlparserhandler. class. getresource ("rain. XML "). getFile (); configpath = java.net. urldecoder. decode (configpath, "UTF-8"); file = new file (configpath ); // specify the encoding used to read the file bufferedreader BR = new bufferedreader (New inputstreamreader (New fileinputstream (file), "gb2312"); string data; stringbuffer sb = new stringbuffer (); while (Data = BR. readline ())! = NULL) {sb. append (data); system. Out. println (data );}

Public static void xmlt () {saxparser parser = NULL; try {parser = saxparserfactory. newinstance (). newsaxparser (); xmlparserhandler handler = new xmlparserhandler (); // parser // ========================================== false data ======================================================================/specify what encoding to read the file //. class. getclassloader () gets a class loader to load. class file, so the Directory should start from the root. Bufferedreader BR = new bufferedreader (New inputstreamreader (xmlparserhandler. class. getclassloader (). getresourceasstream ("WebServices/axis/XML/rain. XML ")," gb2312 "); inputsource is = new inputsource (BR ); // use the relative path of the current class without classloader. // bufferedreader BR = new bufferedreader (// new inputstreamreader (xmlparserhandler. class. // getresourceasstream (". /rain. XML ")," gb2312 "));//. /indicates that the current directory is not written. // inputsource is = new inputsource (BR); // data source parser. parse (is, Handler ); // ================================================ ========================================================== ==} catch (exception E) {system. out. println (E. getmessage ());}}

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.