隨筆 - 3, 文章 - 152, 評論 - 17, 引用 - 0
          數(shù)據(jù)加載中……

          jsp文件操作之讀取篇

          文件操作是網(wǎng)站編程的重要內(nèi)容之一,asp關(guān)于文件操作討論的已經(jīng)很多了,讓我們來看看jsp中是如何實現(xiàn)的。
            這里用到了兩個文件,一個jsp文件一個javabean文件,通過jsp中調(diào)用javabean可以輕松讀取文本文件,注意請放置一個文本文件afile.txt到web根目錄的test目錄下,javabean文件編譯后將class文件放到對應(yīng)的class目錄下(tomcat環(huán)境)。
          Read.jsp

          <html>
          <head>
          <title>讀取一個文件</title>
          </head>
          <body bgcolor="#000000">
          <%--調(diào)用javabean --%>
          <jsp:useBean id="reader" class="DelimitedDataFile" scope="request">
          <jsp:setProperty name="reader" property="path" value="/test/afile.txt" />
          </jsp:useBean>

          <h3>文件內(nèi)容:</h3>

          <p>

          <% int count = 0; %>
          <% while (reader.nextRecord() != -1) { %>
          <% count++; %>
          <b>第<% out.print(count); %>行:</b>
          <% out.print(reader.returnRecord()); %><br>    
          <% } %>
          </p>
          </body>
          </html>


          //DelimitedDataFile.java bean文件源代碼
          //導入java包
          import java.io.*;
          import java.util.StringTokenizer;

          public class DelimitedDataFile
          {

          private String currentRecord = null;
          private BufferedReader file;
          private String path;
          private StringTokenizer token;
          //創(chuàng)建文件對象
          public DelimitedDataFile()
          {
               file = new BufferedReader(new InputStreamReader(System.in),1);
          }
          public DelimitedDataFile(String filePath) throws FileNotFoundException
          {
              
               path = filePath;
               file = new BufferedReader(new FileReader(path));
          }
               //設(shè)置文件路徑
               public void setPath(String filePath)
                  {
                      
                      path = filePath;
          try {
          file = new BufferedReader(new
          FileReader(path));
          } catch (FileNotFoundException e) {
                      System.out.println("file not found");
                      }
              
                  }
          //得到文件路徑
               public String getPath() {
                  return path;
          }
          //關(guān)閉文件
          public void fileClose() throws IOException
          {
              
               file.close();
          }
          //讀取下一行記錄,若沒有則返回-1
          public int nextRecord()
          {
              
              
               int returnInt = -1;
               try
               {
               currentRecord = file.readLine();
               }
              
               catch (IOException e)
               {
               System.out.println("readLine problem, terminating.");
               }
              
               if (currentRecord == null)
               returnInt = -1;
               else
               {
               token = new StringTokenizer(currentRecord);
               returnInt = token.countTokens();
               }
               return returnInt;
          }

              //以字符串的形式返回整個記錄
          public String returnRecord()
          {

          return currentRecord;
          }
          }

          posted on 2005-02-20 11:03 閱讀(152) 評論(0)  編輯  收藏 所屬分類: J2ee

          主站蜘蛛池模板: 昭通市| 石楼县| 锦州市| 天等县| 田林县| 五莲县| 沧州市| 安徽省| 合山市| 江油市| 夏河县| 永济市| 怀仁县| 张家港市| 郴州市| 梁河县| 田林县| 吴川市| 凤冈县| 上饶县| 清水河县| 北票市| 温宿县| 错那县| 永丰县| 抚远县| 普兰县| 崇仁县| 华宁县| 云安县| 斗六市| 广汉市| 卓资县| 玛多县| 汪清县| 凤山县| 尼勒克县| 怀仁县| 阿城市| 兴和县| 公主岭市|