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(newFileReader(path));} catch (FileNotFoundException e) { System.out.println("file not found"); } } //得到文件路径 public String getPath() { return path;} //关闭文件public void fileClose() throws IOException{ file.close();} //读取下一行记录,若没有则返回-1public 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;} }
为了对文件操作有全面了解,请看下一篇$#@60;$#@60;jsp文件操作之写入篇$#@62;$#@62;。
Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved. 闵涛 E_mail:admin@mintao.net(欢迎提供学习资源)
鄂公网安备 42011102001154号
站长:MinTao ICP备案号:鄂ICP备11006601号-18