jsp文件操作之讀取篇
更新時(shí)間:2006年10月13日 00:00:00 作者:
文件操作是網(wǎng)站編程的重要內(nèi)容之一,asp關(guān)于文件操作討論的已經(jīng)很多了,讓我們來(lái)看看jsp中是如何實(shí)現(xiàn)的。
這里用到了兩個(gè)文件,一個(gè)jsp文件一個(gè)javabean文件,通過(guò)jsp中調(diào)用javabean可以輕松讀取文本文件,注意請(qǐng)放置一個(gè)文本文件afile.txt到web根目錄的test目錄下,javabean文件編譯后將class文件放到對(duì)應(yīng)的class目錄下(tomcat環(huán)境)。
Read.jsp
<html>
<head>
<title>讀取一個(gè)文件</title>
</head>
<body bgcolor="#000000">
<%--調(diào)用javabean --%>
<jsp:useBean id="reader" class="DelimitedDataFile" scope="request">
<jsp:setProperty name="reader" property="path" value="/test/afile.txt" />
</jsp:useBean>
<h3>文件內(nèi)容:</h3>
<p>
<% int count = 0; %>
<% while (reader.nextRecord() != -1) { %>
<% count++; %>
<b>第<% out.print(count); %>行:</b>
<% out.print(reader.returnRecord()); %><br>
<% } %>
</p>
</body>
</html>
//DelimitedDataFile.java bean文件源代碼
//導(dǎo)入java包
import java.io.*;
import java.util.StringTokenizer;
public class DelimitedDataFile
{
private String currentRecord = null;
private BufferedReader file;
private String path;
private StringTokenizer token;
//創(chuàng)建文件對(duì)象
public DelimitedDataFile()
{
file = new BufferedReader(new InputStreamReader(System.in),1);
}
public DelimitedDataFile(String filePath) throws FileNotFoundException
{
path = filePath;
file = new BufferedReader(new FileReader(path));
}
//設(shè)置文件路徑
public void setPath(String filePath)
{
path = filePath;
try {
file = new BufferedReader(new
FileReader(path));
} catch (FileNotFoundException e) {
System.out.println("file not found");
}
}
//得到文件路徑
public String getPath() {
return path;
}
//關(guān)閉文件
public void fileClose() throws IOException
{
file.close();
}
//讀取下一行記錄,若沒(méi)有則返回-1
public int nextRecord()
{
int returnInt = -1;
try
{
currentRecord = file.readLine();
}
catch (IOException e)
{
System.out.println("readLine problem, terminating.");
}
if (currentRecord == null)
returnInt = -1;
else
{
token = new StringTokenizer(currentRecord);
returnInt = token.countTokens();
}
return returnInt;
}
//以字符串的形式返回整個(gè)記錄
public String returnRecord()
{
return currentRecord;
}
}
這里用到了兩個(gè)文件,一個(gè)jsp文件一個(gè)javabean文件,通過(guò)jsp中調(diào)用javabean可以輕松讀取文本文件,注意請(qǐng)放置一個(gè)文本文件afile.txt到web根目錄的test目錄下,javabean文件編譯后將class文件放到對(duì)應(yīng)的class目錄下(tomcat環(huán)境)。
Read.jsp
<html>
<head>
<title>讀取一個(gè)文件</title>
</head>
<body bgcolor="#000000">
<%--調(diào)用javabean --%>
<jsp:useBean id="reader" class="DelimitedDataFile" scope="request">
<jsp:setProperty name="reader" property="path" value="/test/afile.txt" />
</jsp:useBean>
<h3>文件內(nèi)容:</h3>
<p>
<% int count = 0; %>
<% while (reader.nextRecord() != -1) { %>
<% count++; %>
<b>第<% out.print(count); %>行:</b>
<% out.print(reader.returnRecord()); %><br>
<% } %>
</p>
</body>
</html>
//DelimitedDataFile.java bean文件源代碼
//導(dǎo)入java包
import java.io.*;
import java.util.StringTokenizer;
public class DelimitedDataFile
{
private String currentRecord = null;
private BufferedReader file;
private String path;
private StringTokenizer token;
//創(chuàng)建文件對(duì)象
public DelimitedDataFile()
{
file = new BufferedReader(new InputStreamReader(System.in),1);
}
public DelimitedDataFile(String filePath) throws FileNotFoundException
{
path = filePath;
file = new BufferedReader(new FileReader(path));
}
//設(shè)置文件路徑
public void setPath(String filePath)
{
path = filePath;
try {
file = new BufferedReader(new
FileReader(path));
} catch (FileNotFoundException e) {
System.out.println("file not found");
}
}
//得到文件路徑
public String getPath() {
return path;
}
//關(guān)閉文件
public void fileClose() throws IOException
{
file.close();
}
//讀取下一行記錄,若沒(méi)有則返回-1
public int nextRecord()
{
int returnInt = -1;
try
{
currentRecord = file.readLine();
}
catch (IOException e)
{
System.out.println("readLine problem, terminating.");
}
if (currentRecord == null)
returnInt = -1;
else
{
token = new StringTokenizer(currentRecord);
returnInt = token.countTokens();
}
return returnInt;
}
//以字符串的形式返回整個(gè)記錄
public String returnRecord()
{
return currentRecord;
}
}
相關(guān)文章
JBuilder2005單元測(cè)試體驗(yàn)之測(cè)試配置
JBuilder2005單元測(cè)試體驗(yàn)之測(cè)試配置...2006-10-10JSP刷新頁(yè)面表單重復(fù)提交問(wèn)題解決辦法分享
最近做項(xiàng)目中有多附件上傳及信息多次添加功能,其中就遇到了刷新頁(yè)面導(dǎo)致上次提交的數(shù)據(jù)重復(fù)保存的問(wèn)題,以下是解決方法,有需要的朋友可以參考一下2013-10-10詳解hibernate自動(dòng)創(chuàng)建表的配置
這篇文章主要介紹了詳解hibernate自動(dòng)創(chuàng)建表的配置的相關(guān)資料,需要的朋友可以參考下2017-06-06JSP開(kāi)發(fā)之Struts2實(shí)現(xiàn)下載功能的實(shí)例
這篇文章主要介紹了JSP開(kāi)發(fā)之Struts2實(shí)現(xiàn)下載功能的實(shí)例的相關(guān)資料,這里提供實(shí)現(xiàn)代碼幫助大家實(shí)現(xiàn)這樣的功能,希望能幫助到大家,需要的朋友可以參考下2017-08-08Spring組件自動(dòng)掃描詳解及實(shí)例代碼
這篇文章主要介紹了Spring組件自動(dòng)掃描詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02JSP由淺入深(10)—— Beans and Forms處理
JSP由淺入深(10)—— Beans and Forms處理...2006-10-10JSP實(shí)用教程之簡(jiǎn)易文件上傳組件的實(shí)現(xiàn)方法(附源碼)
文件上傳是我們?cè)谌粘i_(kāi)發(fā)中經(jīng)常遇到的一個(gè)功能,下面這篇文章主要給大家介紹了關(guān)于JSP實(shí)用教程之簡(jiǎn)易文件上傳組件的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面來(lái)一起看看吧。2017-07-07