?? 0040.htm
字號:
<html>
<head>
<title>新時代軟件教程:操作系統 主頁制作 服務器 設計軟件 網絡技術 編程語言 文字編輯</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋體}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1 {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>jsp文件操作之讀取篇</strong></big></p>
<div align="right">摘自《jsp中國論壇 http://jspbbs.yeah.net》</div>
<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>
</table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -