Java bufferedreader reads webpages and files of different encoding types.

Source: Internet
Author: User
------ Solution ---------------------------------------------------------- respectively call the following two methods to see what is output
Java code
Public static void readgbkcodingfile (string filename) {// read the content of the GBK file bufferedreader BR = NULL; try {BR = new bufferedreader (New inputstreamreader (New fileinputstream (filename ), "GBK"); string line = NULL; while (line = BR. readline ())! = NULL) {system. Out. println (line) ;}} catch (exception e) {}finally {try {If (BR! = NULL) Br. close ();} catch (ioexception e) {}} public static void readutfcodingfile (string filename) {// read the contents of the UTF-8 file bufferedreader BR = NULL; try {BR = new bufferedreader (New inputstreamreader (New fileinputstream (filename), "UTF-8"); string line = NULL; while (line = BR. readline ())! = NULL) {system. Out. println (line) ;}} catch (exception e) {}finally {try {If (BR! = NULL) Br. Close ();} catch (ioexception e) {}}------ solution ---------------------------------------------------------- if you want to convert the file format, try the following two methods:
Java code
Public static void convertgbkfiletoutffile (string srcfilename, string destfilename) {// convert the GBK file to the UTF file bufferedreader BR = NULL; bufferedwriter BW = NULL; try {BR = new bufferedreader (New inputstreamreader (New fileinputstream (srcfilename), "GBK"); BW = new bufferedwriter (New outputstreamwriter (New fileoutputstream (destfilename ), "UTF-8"); string line = NULL; while (line = BR. readline ())! = NULL) {BW. Write (line); BW. newline () ;}} catch (exception e) {} finally {try {If (BR! = NULL) Br. Close (); If (BW! = NULL) BW. close ();} catch (ioexception e) {}} public static void convertutffiletogbkfile (string srcfilename, string destfilename) {// convert the GBK file to the UTF file bufferedreader BR = NULL; bufferedwriter BW = NULL; try {BR = new bufferedreader (New inputstreamreader (New fileinputstream (srcfilename ), "UTF-8"); BW = new bufferedwriter (New outputstreamwriter (New fileoutputstream (destfilename), "GBK"); ST Ring Line = NULL; while (line = Br. Readline ())! = NULL) {BW. Write (line); BW. newline () ;}} catch (exception e) {} finally {try {If (BR! = NULL) Br. Close (); If (BW! = NULL) BW. Close () ;}catch (ioexception e ){}}}
Source: http://www.myexception.cn/j2se/38591.html
 
  
According to my own test, "GBK" is case sensitive.

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.