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

📄 file.htm

📁 java开发实例 多个jsp开发实例
💻 HTM
📖 第 1 页 / 共 2 页
字号:
      <br> 
      &lt;/p><br> 
      &lt;/body><br> 
      &lt;/html><br> 
      <br> 
      <br> 
      import java.io.*;<br> 
      <br> 
      /**<br> 
      * WriteOver.java&nbsp;<br>
      * Written by Morgan Catlin Email: mfcatlin@csclub2.stthomas.edu<br> 
      *  August 19, 1999<br> 
      *&nbsp;<br>
      * Variables:<br> 
      *  String path = path to file to write to (ie. /you/afile.txt)<br> 
      *  String something = something to write to the file<br> 
      *&nbsp;<br>
      * Methods:<br> 
      *  void setPath() = sets path<br> 
      *  String getPath() = returns path<br> 
      *  void setSomething() = sets something<br> 
      *  String getSomething() = returns something<br> 
      *  String writeSomething() = writes something to the path,&nbsp;<br>
      *   returns a message that indicates success or failure(an Exception)<br> 
      */<br> 
      <br> 
      public class WriteOver {<br> 
       &nbsp;<br>
        private String path;<br> 
        private String something;<br> 
       &nbsp;<br>
        public WriteOver() {<br> 
         path = null;<br> 
         something = "Default message";<br> 
        } // constructor WriteOver<br> 
       &nbsp;<br>
        /**<br> 
        * Mutator for the path property<br> 
        */<br>
        public void setPath(String apath) {<br> 
         path = apath;<br> 
        } // mutator setPath<br> 
       &nbsp;<br>
        /**<br> 
        * Accessor for the path property<br> 
        */<br>
        public String getPath() {<br> 
         return path;<br> 
        } // accessor getPathClient<br> 
       &nbsp;<br>
        /**<br> 
        * Mutator for the something property<br> 
        */<br>
        public void setSomething(String asomething) {<br> 
         something = asomething;<br> 
        } // mutator setSomething<br> 
       &nbsp;<br>
        /**<br> 
        * Accessor for the something property<br> 
        */<br>
        public String getSomething() {<br> 
         return something;<br> 
        } // accessor getSomething<br> 
         <br>
        /**<br> 
        * This method writes something to the path<br> 
        */<br>
        public String writeSomething() {<br> 
         try {<br> 
          &nbsp;<br>
           File f = new File(path);<br> 
           PrintWriter out = new PrintWriter(new FileWriter(f));<br> 
          &nbsp;<br>
           out.print(this.getSomething() + "\n");<br> 
           out.close();<br> 
           return "Alles ist Gut.";<br> 
         } catch (IOException e) {<br> 
           return e.toString();<br> 
         }    &nbsp;<br>
        } // method writeSomething&nbsp;<br>
      } // class WriteOver<br> 
      <br> 
      <br> 
      <font color="#FF0000"><b>jsp文件操作之追加篇<br>
      </b></font><br>
      如何用jsp将数据追加到一个文件中,下面是实现方法<br>
      <br>
      <b>writeAppend.jsp&nbsp;<br>
      </b><br>
      &lt;html><br>
      &lt;head><br>
      &lt;title>Append a file&lt;/title><br> 
      &lt;/head><br> 
      &lt;body bgcolor="#000000"><br> 
      <br> 
      &lt;jsp:useBean id="writer" class="WriteAppend" 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 WriteAppend" /><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> 
      <br> 
      &lt;/p><br> 
      &lt;/body><br> 
      &lt;/html><br> 
      <br> 
      WriteAppend.java<br> 
      import java.io.*;<br> 
      <br> 
      /**<br> 
      * WriteAppend.java&nbsp;<br>
      * Written by Morgan Catlin Email: mfcatlin@csclub2.stthomas.edu<br> 
      *  August 19, 1999<br> 
      *&nbsp;<br>
      * Variables:<br> 
      *  String path = path to file to write to (ie. /you/afile.txt)<br> 
      *  String something = something to write to the file<br> 
      *&nbsp;<br>
      * Methods:<br> 
      *  void setPath() = sets path<br> 
      *  String getPath() = returns path<br> 
      *  void setSomething() = sets something<br> 
      *  String getSomething() = returns something<br> 
      *  String writeSomething() = writes something to the path,&nbsp;<br>
      *   returns a message that indicates success or failure(an Exception)<br> 
      */<br> 
      <br> 
      public class WriteAppend {<br> 
       &nbsp;<br>
        private String path;<br> 
        private String something;<br> 
       &nbsp;<br>
        public WriteAppend() {<br> 
         path = null;<br> 
         something = "Default message";<br> 
        } // constructor WriteAppend<br> 
       &nbsp;<br>
        /**<br> 
        * Mutator for the path property<br> 
        */<br>
        public void setPath(String apath) {<br> 
         path = apath;<br> 
        } // mutator setPath<br> 
       &nbsp;<br>
        /**<br> 
        * Accessor for the path property<br> 
        */<br>
        public String getPath() {<br> 
         return path;<br> 
        } // accessor getPathClient<br> 
       &nbsp;<br>
        /**<br> 
        * Mutator for the something property<br> 
        */<br>
        public void setSomething(String asomething) {<br> 
         something = asomething;<br> 
        } // mutator setSomething<br> 
       &nbsp;<br>
        /**<br> 
        * Accessor for the something property<br> 
        */<br>
        public String getSomething() {<br> 
         return something;<br> 
        } // accessor getSomething<br> 
         <br>
        /**<br> 
        * This method writes something to the path<br> 
        */<br>
        public String writeSomething() {<br> 
         try {<br> 
          &nbsp;<br>
          FileWriter theFile = new FileWriter(path,true);<br> 
          PrintWriter out = new PrintWriter(theFile);<br> 
          out.print(something + "\n");<br> 
          out.close();<br>
          theFile.close();<br>
          return "Das war sehr gut!";<br> 
         } catch (IOException e) {<br> 
           return e.toString();<br> 
         }    &nbsp;<br>
        } // method writeSomething&nbsp;<br>
      } // class WriteAppend<br> 
      <br> 
      (全文完) <br>  
      <br>  
         
    </td>   
  </tr>   
<!-- 以下是底边-->               
<script language="javascript" src="../gapbottom.js"></script>  
</center>    
</body>    

⌨️ 快捷键说明

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