十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章主要介紹jsp文件操作之如何實現(xiàn)讀取操作,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
文件操作是網(wǎng)站編程的重要內(nèi)容之一,asp關于文件操作討論的已經(jīng)很多了,讓我們來看看jsp中是如何實現(xiàn)的。
這里用到了兩個文件,一個jsp文件一個javabean文件,通過jsp中調(diào)用javabean可以輕松讀取文本文件,注意請放置一個文本文件afile.txt到web根目錄的test目錄下,javabean文件編譯后將class文件放到對應的class目錄下(tomcat環(huán)境)。
Read.jsp
<%--調(diào)用javabean>
<% int="" count="0;">
<% while="">
<%>
第<%>行:
<%>
<%>
undefined
//DelimitedDataFile.java bean文件源代碼
//導入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)建文件對象
public DelimitedDataFile()
{
file = new BufferedReader(new InputStreamReader(System.in),1);
}
public DelimitedDataFile(String filePath) throws FileNotFoundException
{
path = filePath;
file = new BufferedReader(new FileReader(path));
}
//設置文件路徑
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;
}
//關閉文件
public void fileClose() throws IOException
{
file.close();
}
//讀取下一行記錄,若沒有則返回-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;
}
//以字符串的形式返回整個記錄
public String returnRecord()
{
return currentRecord;
}
}
以上是“jsp文件操作之如何實現(xiàn)讀取操作”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)-成都網(wǎng)站建設公司行業(yè)資訊頻道!