2015-04-04 107 views
0

你好朋友我有以下數據存儲在文件中。BufferReader無法讀取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(); 
} 

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

我很困惑,爲什麼我的代碼的行爲就像這可能有人plz幫助我解決這個問題。

在此先感謝

+0

readFile使用InputStream.available(),就好像它返回文件大小一樣,但它沒有。閱讀該方法的javadoc。 – 2015-04-04 06:46:33

+0

你的代碼在第一個區塊是好的。你最好把'sb.append(line);'改成'sb.append(line +「\ n」);'。你確定你閱讀的方式可能是錯誤的。你如何確定你得到了什麼?詳細說明'以下是輸出'。什麼輸出? – greenapps 2015-04-04 13:44:00

回答

-2

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

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

對於你的第二個代碼檢測,in.available()只返回一個估計剩餘的字節數,所以不要用來確定輸入的大小。順序讀取流直到達到文件結束爲止最好。如果達到文件結尾,in.read()將返回-1。

+0

'available()'返回可以無阻塞地讀取的字節數*的估計值。* Javadoc *中存在特定的警告*,將*用作流中剩餘的字節數。 – EJP 2015-04-04 07:43:31

+0

@EJP。是。這就是爲什麼我特別說OP應該使用不同的方法來確定文件的結尾,即按順序讀取直到in.read()返回-1。 – Hoopje 2015-04-04 10:03:15