国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

jsp文件操作之讀取篇

2020-06-01 23:25:09
字體:
來源:轉載
供稿:網友
文件操作是網站編程的重要內容之一,asp關于文件操作討論的已經很多了,讓我們來看看jsp中是如何實現的。
  這里用到了兩個文件,一個jsp文件一個javabean文件,通過jsp中調用javabean可以輕松讀取文本文件,注意請放置一個文本文件afile.txt到web根目錄的test目錄下,javabean文件編譯后將class文件放到對應的class目錄下(tomcat環境)。
Read.jsp

<html>
<head>
<title>讀取一個文件</title>
</head>
<body bgcolor="#000000">
<%--調用javabean --%>
<jsp:useBean id="reader" class="DelimitedDataFile" scope="request">
<jsp:setProperty name="reader" property="path" value="/test/afile.txt" />
</jsp:useBean>

<h3>文件內容:</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文件源代碼
//導入java包
import java.io.*;
import java.util.StringTokenizer;

public class DelimitedDataFile
{

private String currentRecord = null;
private BufferedReader file;
private String path;
private StringTokenizer token;
//創建文件對象
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.");
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 闵行区| 长沙县| 巴彦淖尔市| 寿光市| 万载县| 拉萨市| 德令哈市| 平武县| 积石山| 密山市| 静安区| 黑河市| 巢湖市| 大港区| 嫩江县| 广宗县| 贵德县| 乌鲁木齐市| 汕尾市| 龙泉市| 鹤庆县| 平原县| 成安县| 铜鼓县| 晴隆县| 长寿区| 岚皋县| 华亭县| 乌兰察布市| 清水河县| 安庆市| 陆河县| 安庆市| 资讯 | 彭州市| 汽车| 铜山县| 合阳县| 建德市| 汤阴县| 张北县|