欢迎光临
我们一直在努力

jsp文件操作之读取篇-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

jsp文件操作之读取篇(本站文章)

转载请注明来源
来源 jsp中国论坛 http://jspbbs.yeah.net

read.jsp

<html>
<head>
<title>read a file</title>
</head>
<body bgcolor="#000000">

<jsp:usebean id="reader" class="delimiteddatafile" scope="request">
  <jsp:setproperty name="reader" property="path" value="/path/to/afile.txt" />
</jsp:usebean>

<h3>contents of the file:</h3>

<p>

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

import java.io.*;
import java.util.stringtokenizer;

public class delimiteddatafile
{
   /**
    * delimiteddatafile.java
    * written by morgan catlin  email: mfcatlin@csclub2.stthomas.edu
    *   april 6, 1999
    *
    * variables:
    *   string currentrecord = the record the bean is currently working on
    *   bufferedreader file = the file the bean is working with
    *   string path = the path to the file (ie. /home/you/afile.txt)
    *   stringtokenizer token = the currentrecord tokenized
    *
    * methods:
    *   public void setpath() – creates a bufferedreader that reads the file in path
    *   public string getpath() – returns path
    *   public void fileclose() – closes the file that is being read
    *   public int nextrecord() – reads the next record(line) in the file,
    *                              and returns the number of tokens in the record
    *                              or else returns -1 if there arent anymore records
    *   public double returndouble() – returns the next token as a double
    *   public int returnint() – returns the next token as an int
    *   public string returnstring() – returns the next token as a string
    *   public string returnrecord() – returns the entire record as a string
    */
   
          private string           currentrecord = null;
          private bufferedreader   file;
          private string           path;
          private stringtokenizer  token;
   
          public delimiteddatafile()
                {
                    file = new bufferedreader(new inputstreamreader(system.in),1);
                } // constructor 1
          public delimiteddatafile(string filepath) throws filenotfoundexception
                {
                    // gets file
                    path = filepath;
                    file = new bufferedreader(new filereader(path));
                } // constructor delimiteddatafile
    
      public void setpath(string filepath)
        {
            // sets the file
            path = filepath;
                        try {
                             file = new bufferedreader(new
                             filereader(path));
                        } catch (filenotfoundexception e) {
            system.out.println("file not found");
            }
    
        } // method setpath

           public string getpath() {
        return path;
          } // method getpath
   
          public void fileclose() throws ioexception
                {
                    // closes file
                    file.close();
                } // method fileclose
   
          public int nextrecord()
                {
                    // this method reads the next record and returns the number of
                    // tokens or else returns -1
    
                    int returnint = -1;
                    try
                        {
                            currentrecord = file.readline();
                        } // end try
    
                    catch (ioexception e)
                        {
                            system.out.println("readline problem, terminating.");
                        } // end catch
    
                    if (currentrecord == null)
                        returnint = -1;
                    else
                        {
                             token = new stringtokenizer(currentrecord);
                             returnint = token.counttokens();
                        } // end else
                    return returnint;
                } // method nextrecord
   
          public double returndouble()
                {
                    // this method returns the next token as a double
                    double doublereturn = double.valueof(token.nexttoken()).doublevalue();
                    return doublereturn;
                } // method returndouble
   
          public int returnint()
                {
                    // this method returns the next token as an int
                    int returnint = integer.parseint(token.nexttoken());
                    return returnint;
                } // method returnint
   
          public string returnstring()
                {
                    // this method returns the next token as a string
                    string stringreturn = token.nexttoken();
                    return stringreturn;
                } // method returnstring
        
          public string returnrecord()
               {
                       // this method returna the entire record as a string
                       return currentrecord;
               }  // method returnrecord
    } // class delimiteddatafile

转载请注明来源
来源 jsp中国论坛 http://jspbbs.yeah.net

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » jsp文件操作之读取篇-JSP教程,Jsp/Servlet
分享到: 更多 (0)

相关推荐

  • 暂无文章