如何在文本文件中打印中文字符

时间:2017-11-15 07:15:22

标签: java html chinese-locale

我正在使用java和Html代码从文本文件和HTML文件中打印数据,但是在HTML文件中打印垃圾字符

report.reportGenerator("result.txt", reportHTMLFile, testCaseSheetMasterMap, environmentUrl, "01/01/01", "02/02/02");

StringBuilder htmlStringBuilder = new StringBuilder();

        // append html header and title
        //<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
        htmlStringBuilder.append(
                "<html><head> <meta charset=\"UTF-8\"><title>CitiGR Automation Report</title><style>p.padding { padding-top: 2cm;}</style></head>");
        // append body
        htmlStringBuilder.append("<body>");
        htmlStringBuilder.append("<p align=\"left\" style=\"color:red\"><h2><u>" + pageTitle + "</u></h2></p>");
        htmlStringBuilder.append("<p align=\"left\" class=\"padding\" style=\" text:red\"><b>Environment URL : <a href=\""+environmentUrl+"\">"+environmentUrl+"</a>");                                         
        htmlStringBuilder.append("<br><br>Start Run Time: "+startRunTime);                                         
        htmlStringBuilder.append("<br>End  Run Time &nbsp;: "+endRunTime+"</b></p>");               

        /* append 1st table */
        htmlStringBuilder.append(
                "<table border=\"1\" text-align:center><col width=\"50\"><col width=\"200\"><col width=\"200\"><col width=\"100\"><col width=\"100\"><col width=\"100\"><col width=\"100\">");
        // append row - table header
        htmlStringBuilder.append(
                "<tr>"
                + "<td align=\"center\" bgcolor=\"LightBlue\"><b>Si.No.</b></td>"
                + "<td align=\"center\" bgcolor=\"LightBlue\"><b>TestCase Name</b></td>"
                + "<td align=\"center\" bgcolor=\"LightBlue\"><b>Total no. of scripts executed</b></td>"
                + "<td align=\"center\" bgcolor=\"green\"><b>Pass</b></td>"
                + "<td align=\"center\" bgcolor=\"red\"><b>Fail</b></td>"
                + "<td align=\"center\" bgcolor=\"green\"><b>Pass %</b></td>"
                + "<td align=\"center\" bgcolor=\"red\"><b>Fail %</b></td>"
                + "</tr>");

        int count=1;
        for (int i = 0; i < eachTestCaseMenuList.size(); ) {                        
            htmlStringBuilder.append(""
                    + "<tr>"
                    + "<td align=\"center\" >"+count+"</td>"
                    + "<td align=\"center\">"+ eachTestCaseMenuList.get(i) + "</td>"
                    + "<td align=\"center\">"+ eachTestCaseMenuList.get(i+1) + "</td>"
                    + "<td align=\"center\" style=\" color:DarkGreen\">" + eachTestCaseMenuList.get(i+2)+ "</td>"
                    + "<td align=\"center\" style=\" color:red\">" + eachTestCaseMenuList.get(i+3) + "</td>"
                    + "<td align=\"center\" style=\" color:DarkGreen\">" + eachTestCaseMenuList.get(i+4)+ "</td>"
                    + "<td align=\"center\" style=\" color:red\">" + eachTestCaseMenuList.get(i+5) + "</td>"
                    + "</tr>");
            i+=6;   
            count++;
        }       

        htmlStringBuilder.append(
                "<tr>"
                + "<td align=\"center\" bgcolor=\"LightBlue\"></td>"
                + "<td align=\"center\" bgcolor=\"LightBlue\"><b>Total</b></td>"
                + "<td align=\"center\" bgcolor=\"LightBlue\"><b>"+totalTestCaseCount+"</b></td>"
                + "<td align=\"center\" bgcolor=\"green\"><b>"+totalPassCount+"</b></td>"
                + "<td align=\"center\" bgcolor=\"red\"><b>"+totalFailCount+"</b></td>"
                + "<td align=\"center\" bgcolor=\"green\"><b>"+totalPassPercent+" %</b></td>"
                + "<td align=\"center\" bgcolor=\"red\"><b>"+totalFailPercent+" %</b></td>"
                + "</tr>");

enter image description here

3 个答案:

答案 0 :(得分:1)

短:

<meta charset="utf-8" /> 

长:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

答案 1 :(得分:0)

<head>标记中加入编码标记

尝试如下,

<meta charset="utf-8"> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

答案 2 :(得分:0)

这需要进入&lt;头&gt;你网页的一部分。

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

服务器应该发送这样的响应头:

Content-Type: text/html; charset=UTF-8

如果&#34; charset = UTF-8&#34; Content-Type响应标头中缺少bit,或者如果它出现&#34; UTF-8&#34;以外的其他内容,则会导致问题。