BufferedWriter.write() solution to writing Chinese characters

In the process of reading and writing files with BufferedReader and BufferedWriter, it is found that the Chinese characters in the written files will be garbled.
Suppose you want to http://www.baidu.com The content of Baidu home page is replaced by the content you want to customize, and the code is as follows:

/**
     * Get source code through web URL
     * @param getUrl
     */
    public static void catchHtmlCode(String getUrl){

        String filePath = "D:/test/test.html";
        BufferedReader buffreader = null;
        BufferedWriter writer = null;
        try {
            URL u = new URL(getUrl);
            URLConnection connection = u.openConnection();
            InputStream inputStream = connection.getInputStream();

            buffreader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
            StringBuffer buff = new StringBuffer();
            String line;
            while (null != (line = buffreader.readLine())) {
                buff.append(line);
            }
            String html = buff.toString();
            Document doc = Jsoup.parse(html);
            Element body = doc.body();
            Element div = body.select(".head_wrapper").first();
            /*Replace the specified html label content*/
            div.html("<h1 style='font-size:30px'>I have assigned html Label content replaced with empty</h3>");

            writer = new BufferedWriter(new FileWriter(filePath, false));
            writer.write(doc.html());
            writer.flush();

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            FileUtils.closeStream(buffreader);
            FileUtils.closeStream(writer);
        }
    }

When we use the following methods to create a stream, Chinese scrambling may occur. (check the program breakpoint to see that the Chinese characters obtained are free of scrambling. It is about opening the scrambling code after the file is written, which is related to the default open coding setting of the specific generated file.)

writer = new BufferedWriter(new FileWriter(filePath, false));

So we can specify the code when creating the stream, as follows:

writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(filePath)),"utf-8"));

This solves the problem of random code. It can also be set to "GBK" and other formats, depending on your needs.
Operation result:

public static void main(String[] args) {
        catchHtmlCode("http://www.baidu.com");
    }

Technical blog of the original author: https://www.jianshu.com/u/ac4daaeecdfe
After 95, there is a front-end girl who loves reading and making friends. Record the problems encountered in work here, hoping to bring a little help to everyone you see.
Welcome to leave a message.

Posted on Tue, 03 Dec 2019 03:47:56 -0500 by blkraven