BufferReader无法读取java中的完整文件

时间:2015-04-04 06:08:54

标签: java android file fileinputstream

Hello Friends我在商店中存储了以下数据。

 <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head></head>
    <body>
<div id="divOldReceipt" style="width:100%">
    <style>

        .tableStyle{border-collapse: collapse;
               width: 100%; 
                   font-family: Times New Roman; 
               font-size: 17px;
                   border: 1px solid black;}

        .tableStyle th td {border: 1px solid black;}

        .tableStyle td {border: 1px solid black;padding-left:3px;}
    </style>

    <table cellspacing="0" cellpadding="0" style="width: 8in; font-family:Times New Roman;">
                    <tbody>
                        <tr>
                            <td style="width: 20%;text-align:center" rowspan="2">
                                <img style="height: 84px; width: 71px" src={%images_path%}></img>
                            </td>
                            <td style="width: 60%;text-align: center; font-size: 25px;">
                                <b>

                                    CORPORATION OF CHENNAI

                                </b>
                            </td>
                            <td></td>
                        </tr>
                        <tr>
                            <td style="text-align: center; font-size: 18px">
                                <b>

                                    PROPERTY TAX RECEIPT

                                </b>
                            </td>
                            <td></td>
                        </tr>
                        <tr>
                            <td colspan="3">
                                <table class="tableStyle" cellspacing="0" cellpadding="0">
                                    <tbody>
                                        <tr>
                                            <td style="width: 20%"></td>
                                            <td style="width: 20%"></td>
                                            <td style="width: 10%"></td>
                                            <td style="width: 10%"></td>
                                            <td style="width: 40%"></td>
                                        </tr>
                                        <tr>
                                            <td style="height: 25px;">
                                                <b>

                                                    Receipt No:

                                                </b>
                                            </td>
                                            <td colspan="2" style="height: 25px;">
                                                <b>

                                                    {%receiptNo%}

                                                </b>
                                            </td>
                                            <td colspan="2" style="text-align:center;height: 25px;">
                                                <b>

                                                    Receipt Date:   {%Receipt_date%}

                                                </b>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="height: 25px;">
                                                <b>

                                                    Name:

                                                </b>
                                            </td>
                                            <td style="height: 25px;" colspan="4">

                                                {%persone_name%}

                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="height: 25px;">
                                                <b>

                                                    Address:

                                                </b>
                                            </td>
                                            <td style="height: 25px;" colspan="4">

                                               {%address%}

                                            </td>
                                        </tr>
                                        <tr>
                                            <td rowspan="2" style="height: 25px;">
                                                <b>

                                                    Payment Details:

                                                </b>
                                            </td>
                                            <td style="height: 25px;border-right-style:none;">

                                                Description:

                                            </td>
                                            <td colspan="3" style="height: 25px;border-left-style:none;">

                                                New Property Tax Number : {%new_property_tax_no%}

                                                <br>

                                                Old Property Tax Number : {%old_property_tax_no%}

                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="height: 25px;border-right-style:none;">

                                                Paid By:

                                            </td>
                                            <td colspan="3" style="height: 25px;border-left-style:none;">

                                                {%paid_by%}

                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align:center;height: 25px;" colspan="3">
                                                <b>

                                                    Description Head of A/C

                                                </b>
                                            </td>
                                            <td style="text-align:center;height: 25px;" colspan="2">
                                                <b>

                                                    Amount (in Rupees)

                                                </b>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="height: 25px;text-align: center;" colspan="3">

                                                {%installment%}

                                            </td>
                                            <td style="text-align:center;padding-right:5px;height : 25px;" colspan="2">

                                                {%adjistment%}

                                            </td>
                                        </tr>
                                        <tr>
                                            <td style="text-align:right;padding-right:5px;height: 25px;" colspan="3">
                                                <b>

                                                    Total:

                                                </b>
                                            </td>
                                            <td style="text-align:center;padding-right:5px;height : 25px;" colspan="2">
                                                <b>

                                                    {%adjistment%}

                                                </b>
                                            </td>
                                        </tr>

                                        {%Bank Details%}
                                        <tr>
                                            <td colspan="5" style="height: 25px;">

                                                This is computer generated receipt. Signature is not neccessary.Except Online payment the receipt is subject to realisation of cheque.

                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                    </tbody>
                </table>
</div>
   </body>

我想使用以下代码在java中阅读此文件

    public String readTemplateFile(String fileName) throws Exception
  {

     BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
     String line =null;
     StringBuilder sb = new StringBuilder();
     while((line = bufferedReader.readLine())!=null)
     {
         sb.append(line);
     }
     return sb.toString();
 }
  

OR

public String readFile(String filepath) throws IOException {
    File f = new File(filepath);
    if (f.exists()) {
        FileInputStream in = new FileInputStream(f);
        int size = in.available();
        byte c[] = new byte[size];
        for (int i = 0; i < size; i++) {
            c[i] = (byte) in.read();
        }
        String filedata = new String(c, "utf-8");
        return filedata.toString().trim();
    } else {
        return null;
    }
}

但是没有读取完整文件 以下是输出

<!DOCTYPE html>    <html xmlns="http://www.w3.org/1999/xhtml">        <head></head>        <body>   <div id="divOldReceipt" style="width:100%">     <style>                 .tableStyle{border-collapse: collapse;                 width: 100%;                        font-family: Times New Roman;                   font-size: 17px;                    border: 1px solid black;}            .tableStyle th td {border: 1px solid black;}                    .tableStyle td {border: 1px solid black;padding-left:3px;}      </style>                <table cellspacing="0" cellpadding="0" style="width: 8in; font-family:Times New Roman;">                        <tbody>                            <tr>                                <td style="width: 20%;text-align:center" rowspan="2">                                    <img style="height: 84px; width: 71px" src="../Images/Corporation.gif"></img>                                </td>                                <td style="width: 60%;text-align: center; font-size: 25px;">                                    <b>                                        CORPORATION OF CHENNAI                                    </b>                                </td>                                <td></td>                            </tr>                            <tr>                                <td style="text-align: center; font-size: 18px">                                    <b>                                        PROPERTY TAX RECEIPT                                    </b>                                </td>                                <td></td>                            </tr>                            <tr>                                <td colspan="3">                                    <table class="tableStyle" cellspacing="0" cellpadding="0">                                        <tbody>                                            <tr>                                                <td style="width: 20%"></td>                                                <td style="width: 20%"></td>                                                <td style="width: 10%"></td>                                                <td style="width: 10%"></td>                                                <td style="width: 40%"></td>                                            </tr>                                            <tr>                                                <td style="height: 25px;">                                                    <b>                                                        Receipt No:                                                    </b>                                                </td>                                                <td colspan="2" style="height: 25px;">                                                    <b>                                                        {%receiptNo%}                                                    </b>                                                </td>                                                <td colspan="2" style="text-align:center;height: 25px;">                                                    <b>                                                        Receipt Date:   {%Receipt_date%}                                                    </b>                                                </td>                                            </tr>                                            <tr>                                                <td style="height: 25px;">                                                    <b>                                                        Name:                                                    </b>                                                </td>                                                <td style="height: 25px;" colspan="4">                                                    {%persone_name%}                                                </td>                                            </tr>                                            <tr>                                                <td style="height: 25px;">

我很困惑为什么我的代码表现得像这样可以帮助我解决这个问题。

提前致谢

1 个答案:

答案 0 :(得分:-2)

您提到的输出是由第一个还是第二个代码样本生成的?

对于您的第一个代码示例,请注意bufferedReader.readLine()将返回文件的下一行,但会丢弃行尾字符。 sb.append(line)方法调用再次添加 not 这些行尾字符。这可以解释为什么从文件中删除行尾。

对于第二次代码检查,in.available()仅返回流中剩余字节数的估计值,因此不要用来确定输入的大小。最好按顺序读取流,直到达到文件结尾。如果到达文件结尾,in.read()将返回-1。