簡體   English   中英

BufferReader無法讀取Java中的完整文件

[英]BufferReader not able to read full file in java

您好朋友,我有以下數據存儲在文件中。

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

要么

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;">

我感到困惑,為什么我的代碼會如此運行,但有人可以幫助我解決此問題。

提前致謝

您提到的輸出是由第一個還是第二個代碼樣本生成的?

對於您的第一個代碼示例,請注意bufferedReader.readLine()返回文件的下一行,但放棄行尾字符。 sb.append(line)方法調用不會再次添加這些行尾字符。 這可能解釋了為什么從文件中刪除了行尾。

對於您的第二個代碼in.available()in.available()僅返回流中剩余字節數的估計值 ,因此請勿用於確定輸入的大小。 最好順序讀取流,直到到達文件末尾。 如果到達文件末尾,則in.read()將返回-1。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM