// 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 ());}}