⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file.htm

📁 java开发实例 多个jsp开发实例
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="KEYWORDS" content="小龙亭工作室之JSP实践之旅">
<title>小龙亭工作室之JSP实践之旅</title>
<link rel="stylesheet" href="../jsp.css" type="text/css">
</head>

<body topmargin="0" leftmargin="0" rightmargin="0">
<div align="center"><!--以下开始小龙亭标题 -->
<script language="javascript" src="../gaptitle.js"></script>
<!--以下开始主题索引td和文章区td -->
<div
align="center"><center>

<table width="100%">
  <tr>
    <td colspan="2" height="10"></td>
  </tr>
  <tr>
    <td width="20%" style="border-right: 1px solid red" valign="top" height="227"><script
    language="javascript" src="manualindex.js"></script> </td>
    <td width="80%" valign="top" height="227"> 
      <p align="center"><b><font color="#FF0000" size="3">JSP文件操作:读取,写入和追加</font></b>
      <p> 
      <p>来源 jsp中国论坛 <a href="http://jspbbs.yeah.net"> http://jspbbs.yeah.net</a>
      <p>作者: ASP3000<br>
      <p><font color="#FF0000"><b>jsp文件操作之读取篇<br>
      </b></font><br>
      <b>Read.jsp<br>
      </b><br>
      &lt;html><br>
      &lt;head><br>
      &lt;title>Read a file&lt;/title><br> 
      &lt;/head><br> 
      &lt;body bgcolor="#000000"><br> 
      <br> 
      &lt;jsp:useBean id="reader" class="DelimitedDataFile" scope="request"><br> 
       &lt;jsp:setProperty name="reader" property="path" value="/path/to/afile.txt" /><br> 
      &lt;/jsp:useBean><br> 
      <br> 
      &lt;h3>Contents of the file:&lt;/h3><br> 
      <br> 
      &lt;p><br> 
      <br> 
      &lt;% int count = 0; %>&nbsp;<br>
      &lt;% while (reader.nextRecord() != -1) { %><br> 
       &lt;% count++; %>   <br>
       &lt;b>Line &lt;% out.print(count); %>:&lt;/b> &lt;% out.print(reader.returnRecord()); %>&lt;br>    <br>
      &lt;% } %> <br>
      &lt;/p><br>
      &lt;/body><br>
      &lt;/html><br>
      <br>
      <br>
      <br>
      import java.io.*;<br> 
      import java.util.StringTokenizer;<br> 
      <br> 
      public class DelimitedDataFile&nbsp;<br>
      {<br>
        /**<br> 
        * DelimitedDataFile.java<br> 
        * Written by Morgan Catlin Email: mfcatlin@csclub2.stthomas.edu<br> 
        *  April 6, 1999<br> 
        *&nbsp;<br>
        * Variables:<br> 
        *  String currentRecord = the record the bean is currently working on<br> 
        *  BufferedReader file = the file the bean is working with<br> 
        *  String path = the path to the file (ie. /home/you/afile.txt)<br> 
        *  StringTokenizer token = the currentRecord tokenized<br> 
        *&nbsp;<br>
        * Methods:<br> 
        *  public void setPath() - creates a BufferedReader that reads the file in path<br> 
        *  public String getPath() - returns path<br> 
        *  public void fileClose() - closes the file that is being read<br> 
        *  public int nextRecord() - reads the next record(line) in the file,&nbsp;<br>
        *               and returns the number of tokens in the record&nbsp;<br>
        *               or else returns -1 if there aren't anymore records<br> 
        *  public double returnDouble() - returns the next token as a double<br> 
        *  public int returnInt() - returns the next token as an int<br> 
        *  public String returnString() - returns the next token as a String<br> 
        *  public String returnRecord() - returns the entire record as a String<br> 
        */<br>
       &nbsp;<br>
           private String      currentRecord = null;<br> 
           private BufferedReader  file;<br> 
           private String      path;<br> 
           private StringTokenizer token;<br>
       &nbsp;<br>
           public DelimitedDataFile()<br> 
              {<br>
                  file = new BufferedReader(new InputStreamReader(System.in),1);<br> 
              } // constructor 1&nbsp;<br>
           public DelimitedDataFile(String filePath) throws FileNotFoundException<br> 
              {<br>
                  // gets file<br> 
                  path = filePath;<br> 
                  file = new BufferedReader(new FileReader(path));<br> 
              } // constructor DelimitedDataFile<br> 
          <br>
           public void setPath(String filePath)<br> 
              {<br>
                  // sets the file<br> 
                  path = filePath;<br> 
                  try {<br> 
                     file = new BufferedReader(new<br> 
                     FileReader(path));<br> 
                  } catch (FileNotFoundException e) {<br> 
                  System.out.println("file not found");<br> 
                  }<br>
          <br>
              } // method setPath<br> 
      <br> 
              public String getPath() {<br> 
              return path;<br> 
           } // method getPath<br> 
       &nbsp;<br>
           public void fileClose() throws IOException<br> 
              {<br>
                  // closes file<br> 
                  file.close();<br>
              } // method fileClose<br> 
       &nbsp;<br>
           public int nextRecord()<br> 
              {<br>
                  // this method reads the next record and returns the number of<br> 
                  // tokens or else returns -1<br> 
          <br>
                  int returnInt = -1;<br> 
                  try<br>
                    {<br>
                      currentRecord = file.readLine();<br> 
                    } // end try<br> 
          <br>
                  catch (IOException e)<br> 
                    {<br>
                      System.out.println("readLine problem, terminating.");<br> 
                    } // end catch<br> 
          <br>
                  if (currentRecord == null)<br> 
                    returnInt = -1;<br> 
                  else<br>
                    {<br>
                       token = new StringTokenizer(currentRecord);<br> 
                       returnInt = token.countTokens();<br> 
                    } // end else<br> 
                  return returnInt;<br> 
              } // method nextRecord<br> 
       &nbsp;<br>
           public double returnDouble()<br> 
              {<br>
                  // this method returns the next token as a double<br> 
                  double doubleReturn = Double.valueOf(token.nextToken()).doubleValue();<br> 
                  return doubleReturn;<br> 
              } // method returnDouble<br> 
       &nbsp;<br>
           public int returnInt()<br> 
              {<br>
                  // this method returns the next token as an int<br> 
                  int returnint = Integer.parseInt(token.nextToken());<br> 
                  return returnint;<br> 
              } // method returnInt<br> 
       &nbsp;<br>
           public String returnString()<br> 
              {<br>
                  // this method returns the next token as a String<br> 
                  String stringReturn = token.nextToken();<br> 
                  return stringReturn;<br> 
              } // method returnString<br> 
            <br>
           public String returnRecord()<br> 
              {<br> 
                  // this method returna the entire record as a string<br> 
                  return currentRecord;<br> 
              } // method returnRecord<br> 
        } // class DelimitedDataFile<br> 
      <br> 
      <font color="#FF0000"><b>jsp文件操作之写入篇<br>
      </b></font><b>WriteOver.Jsp<br>
      </b><br>
      &lt;html><br>
      &lt;head><br>
      &lt;title>Write over a file&lt;/title><br> 
      &lt;/head><br> 
      &lt;body bgcolor="#000000"><br> 
      <br> 
      &lt;jsp:useBean id="writer" class="WriteOver" scope="request"><br> 
      &lt;jsp:setProperty name="writer" property="path" value="/path/to/afile.txt" /><br> 
      &lt;jsp:setProperty name="writer" property="something" value="Something already set as a property in WriteOver" /><br> 
      &lt;/jsp:useBean><br> 
      <br> 
      &lt;h3>Write to the file&lt;/h3><br> 
      <br> 
      &lt;p><br> 
      <br> 
      &lt;% writer.setSomething("Something to write to the file"); %><br> 
      &lt;% out.print(writer.getSomething()); %><br> 
      <br> 
      &lt;% out.print(writer.writeSomething()); %><br> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -