📄 file.htm
字号:
<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>
<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>
<font color="#FF0000"><b>jsp文件操作之写入篇<br>
</b></font><b>WriteOver.Jsp<br>
</b><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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -