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

📄 76.html

📁 关于jsp的一些好文章 主要介绍一些关于JSP的应用技巧方面的东西
💻 HTML
📖 第 1 页 / 共 2 页
字号:

<STYLE type=text/css>
<!--
body,td { font-size:9pt;}
hr { color: #000000; height: 1px}
-->
</STYLE>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD><TITLE>精选文章 >> 文件操作 >> JSP文件操作:读取,写入和追加</title>
</head>
<body >

<p><IMG SRC="../image/jsp001_middle_logo.gif" WIDTH="180" HEIGHT="60" BORDER=0 ALT=""></p>

<table width=100% bgcolor="#cccccc" align=center cellpadding="2" cellspacing="0" border=1 bordercolorlight="#000000" bordercolordark="#FFFFFF">
<tr bgcolor="#EFF8FF"><td>
<a href=http://www.jsp001.com/list_thread.php?int_attribute=2>精选文章</a>
>> <a href=http://www.jsp001.com/list_thread.php?forumid=16&int_attribute=2>文件操作</a>
>> JSP文件操作:读取,写入和追加 [<a href=http://www.jsp001.com/forum/showthread.php?goto=newpost&threadid=76>查看别人的评论</a>]<br>

<hr><p>由 webmaster 发布于: 2001-01-20 16:54</p><p> </p><p>来源 jsp中国论坛 <a href="http://jspbbs.yeah.net" target=_blank>http://jspbbs.yeah.net</a> <br><br>作者: ASP3000<br><br><br>jsp文件操作之读取篇<br><br>Read.jsp<br><br>&lt;html&gt;<br>&lt;head&gt;<br>&lt;title&gt;Read a file&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body bgcolor="#000000"&gt;<br><br>&lt;jsp:useBean id="reader" class="DelimitedDataFile" scope="request"&gt;<br> &lt;jsp:setProperty name="reader" property="path" value="/path/to/afile.txt" /&gt;<br>&lt;/jsp:useBean&gt;<br><br>&lt;h3&gt;Contents of the file:&lt;/h3&gt;<br><br>&lt;p&gt;<br><br>&lt;% int count = 0; %&gt; <br>&lt;% while (reader.nextRecord() != -1) { %&gt;<br> &lt;% count++; %&gt;   <br> &lt;b&gt;Line &lt;% out.print(count); %&gt;:&lt;/b&gt; &lt;% out.print(reader.returnRecord()); %&gt;&lt;br&gt;    <br>&lt;% } %&gt; <br>&lt;/p&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<br><br><br><br>import java.io.*;<br>import java.util.StringTokenizer;<br><br>public class DelimitedDataFile <br>{<br>  /**<br>  * DelimitedDataFile.java<br>  * Written by Morgan Catlin Email: mfcatlin@csclub2.stthomas.edu<br>  *  April 6, 1999<br>  * <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>  * <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, <br>  *               and returns the number of tokens in the record <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>  <br>     private String      currentRecord = null;<br>     private BufferedReader  file;<br>     private String      path;<br>     private StringTokenizer token;<br>  <br>     public DelimitedDataFile()<br>        {<br>            file = new BufferedReader(new InputStreamReader(System.in),1);<br>        } // constructor 1 <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>  <br>     public void fileClose() throws IOException<br>        {<br>            // closes file<br>            file.close();<br>        } // method fileClose<br>  <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>  <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>  <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>  <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>jsp文件操作之写入篇<br>WriteOver.Jsp<br><br>&lt;html&gt;<br>&lt;head&gt;<br>&lt;title&gt;Write over a file&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body bgcolor="#000000"&gt;<br><br>&lt;jsp:useBean id="writer" class="WriteOver" scope="request"&gt;<br>&lt;jsp:setProperty name="writer" property="path" value="/path/to/afile.txt" /&gt;<br>&lt;jsp:setProperty name="writer" property="something" value="Something already set as a property in WriteOver" /&gt;<br>&lt;/jsp:useBean&gt;<br><br>&lt;h3&gt;Write to the file&lt;/h3&gt;<br><br>&lt;p&gt;<br><br>&lt;% writer.setSomething("Something to write to the file"); %&gt;<br>&lt;% out.print(writer.getSomething()); %&gt;<br><br>&lt;% out.print(writer.writeSomething()); %&gt;<br><br>&lt;/p&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<br>

⌨️ 快捷键说明

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