當前位置:編程學習大全網 - 源碼下載 - 基於JSP的web題庫系統中的隨機抽取試題。數據庫用的my sql。

基於JSP的web題庫系統中的隨機抽取試題。數據庫用的my sql。

這裏用到了兩個文件,壹個jsp文件壹個javabean文件,通過jsp中調用javabean可以輕松讀取文本文件,註意請放置壹個文本文件afile.txt到web根目錄的test目錄下,javabean文件編譯後將class文件放到對應的class目錄下(tomcat環境)。

Read.jsp

$#@60;html$#@62;

$#@60;head$#@62;

$#@60;title$#@62;讀取壹個文件$#@60;/title$#@62;

$#@60;/head$#@62;

$#@60;body bgcolor="#000000"$#@62;

$#@60;%--調用javabean --%$#@62;

$#@60;jsp:useBean id="reader" class="DelimitedDataFile" scope="request"$#@62;

$#@60;jsp:setProperty name="reader" property="path" value="/test/afile.txt" /$#@62;

$#@60;/jsp:useBean$#@62;

$#@60;h3$#@62;文件內容:$#@60;/h3$#@62;

$#@60;p$#@62;

$#@60;% int count = 0; %$#@62;

$#@60;% while (reader.nextRecord() != -1) { %$#@62;

$#@60;% count++; %$#@62;

$#@60;b$#@62;第$#@60;% out.print(count); %$#@62;行:$#@60;/b$#@62;

$#@60;% out.print(reader.returnRecord()); %$#@62;$#@60;br$#@62;    

$#@60;% } %$#@62;

$#@60;/p$#@62;

$#@60;/body$#@62;

$#@60;/html$#@62;

//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)

    {

        

        ath = 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;

}

}

  • 上一篇:大話西遊2大鬧天空,猛壯士用什麽武器強法?
  • 下一篇:易語言如何獲取鼠標在窗口中的位置
  • copyright 2024編程學習大全網