📄 76.html
字号:
<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><html><br><head><br><title>Read a file</title><br></head><br><body bgcolor="#000000"><br><br><jsp:useBean id="reader" class="DelimitedDataFile" scope="request"><br> <jsp:setProperty name="reader" property="path" value="/path/to/afile.txt" /><br></jsp:useBean><br><br><h3>Contents of the file:</h3><br><br><p><br><br><% int count = 0; %> <br><% while (reader.nextRecord() != -1) { %><br> <% count++; %> <br> <b>Line <% out.print(count); %>:</b> <% out.print(reader.returnRecord()); %><br> <br><% } %> <br></p><br></body><br></html><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><html><br><head><br><title>Write over a file</title><br></head><br><body bgcolor="#000000"><br><br><jsp:useBean id="writer" class="WriteOver" scope="request"><br><jsp:setProperty name="writer" property="path" value="/path/to/afile.txt" /><br><jsp:setProperty name="writer" property="something" value="Something already set as a property in WriteOver" /><br></jsp:useBean><br><br><h3>Write to the file</h3><br><br><p><br><br><% writer.setSomething("Something to write to the file"); %><br><% out.print(writer.getSomething()); %><br><br><% out.print(writer.writeSomething()); %><br><br></p><br></body><br></html><br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -