JSPCN主页 | JSP空间 | 网站制作 | JSP下载 | JSP论坛 | JSP教程 | 关于JSPCN | 联系我们
JSP虚拟主机,jsp空间,java空间,java虚拟空间,详细请点击进入
做最专业的JSP中文网站 当前位置首页--JSP技术--文件操作  
文章搜索:
关键字 标题
  
JSP中文网内容管理系统(JCMS)
JSP虚拟主机
网络笔记本
网摘,图片,笔记收藏
虚拟服务器

JSPCN文章目录分类
JSP配置[219]JSP基础[136]
中文问题[69]上传问题[27]
JAVABEAN[46]数据库[212]
文件操作[126]图片声音[17]
JSP其他[57]时间相关[16]
JAVAMAIL[72]STRUTS[144]
开发工具[28]教程系列[157]
JSP实例[89]
JAVA基础[421]APPLET[78]
JAVA网络[179]Applica[115]
Servlet[98]XML[163]
J2ME[257]J2EE[374]
考试相关[63]JAVA线程[90]
EJB[261]Swing[26]
Java API[141]声音图片[28]
异常处理[33]JAVA实例[290]
JAVA类[139]SUN[89]
Hibernate[6]JMX[8]
Spring[34]
本版推荐文章 
本版热点文章 
相关文章链接 
 
JSP读取Text文件
作者:     文章来源:
访问次数:788次     加入时间:2007年02月23日
附有JSP源码(TextFileReader.jsp)及JavaBean (TextFileReader.java 使用前需加以编译)

我们使用了较早期的jswdk,所以我们可以确信你也可以直接使用这些代码。

TextFileReader.java是一个bean, TextFileReader.jsp则是jsp文件。如果你也使用d jswdk,并使用相同的library environment,可叫bean文件放在jswdk1-0eaexamplesjsp下的textfileaccess目录(你可以创建它),jsp文件放在jswdk1-0eaexamplesWeb-infjspbeanstextfileaccess目录,你也必须创建它。

我们使用的jsp文件并不包含太多的java代码,主要的代码放在bean中。由此我们也可以看到JSP和JavaBean的基本联系。
对于有经验的开发者:

在"header"信息中我们要申明要使用、识别哪一个bean,并设置其属性。

首先,我们导入bean,如果你的jswdk设置正确并已经将文件放在上述位置,那么找到 resource应该没有问题。page命令的意思是它将为整个jsp页面来进行导入。

<%@ page import ="textfileaccess.TextFileReader" %>

告诉编译器我们将使用一个bean,以及如何识别它,并进行初始化(instansiate)。 scope指明被申明的对象对当前页有效。



然后我们决定要设置那些属性。这里是"FileName"。因为我们要使用Bean的setFileName 方法。所以Bean的名字必须包含。



那就是header信息,现在我们开始实际的HTML页面。



Read a text file





现在我们开始编写一些Java脚本。首先检查文件名是否已经设置好。如果设好了,我们就显示文件,否则我们要转到另一个页面。

<%if(file_reader.getFileName() != "") { %>

file_reader是一个bean,所以我们可以用Java类来存取它。 :-)现在我们得到文件名称!


文件名称是: '<% out.println(file_reader.getFileName()); %>' :



文件内容,如果为空的话:

<%if (file_reader.getContent() != null) { %>

我们可以建立一个textarea (HTML) 并用getRows()和getColumns() 方法来调节到合适的位置。然后将文件内容放入。



cols=<%= file_reader.getColumns()%>id= textarea1name= textarea1>< /FONT>

<%out.println(file_reader.getContent()); %>





如果文件为空,那么一定是发生了错误,我们将得到出错信息:

<% }else { %>

<% out.println(file_reader.getErrorMessage()); %>

<% } %>





重置所有值并返回主页:

<% file_reader.reset(); %>

Do you want to look at another file?

<% }else { %>

文件名为空,则显示出错页面。

欢迎加入这里:'Read a file in JSP'


这个示例在textarea中简单地显示了文件内容?lt;p>

请填写你想看到什么文件。并确信键入了完整的路径。



建立带textboxbutton的form。注意我们不必定义form的action,因为使用了同一个页面。并注意textbox中要填入文件名字。

< /FONT>

FileName? < /FONT>





<% } %>







jsp文件完成了。在仔细看以下Bean中的Java代码。我假设你们中的大多数都熟悉java,否则你怎么会加入JSP的行列。:-)

**************JSP代码: TextFileReader.jsp


<%@ page import = "textfileaccess.TextFileReader" %>





Read a text file



<% if (file_reader.getFileName() != "") { %>

The content of the file '<% out.println(file_reader.getFileName()); %>' :



<% if (file_reader.getContent() != null) { %>





<% } else { %>
<% out.println(file_reader.getErrorMessage()); %>

<% } %>





<% file_reader.reset(); %>
Do you want to look at another file?


<% } else { %>

Welcome to the 'Read a file in JSP' example.

The example simply shows the file in a textarea.


Please fill out what file you want to look at. Be sure to type the complete path.




FileName?



<% } %>






**************Java Bean TextFileReader.java
package textfileaccess;

import java.io.*;
import java.awt.event.*;
import java.util.*;

/**
* TextFileReader is a bean that provides the basic functionality for
* reading a textfile.
*/
public class TextFileReader {

private String fileName, errorMessage;
private int columns, rowCount;

/**
* Constructs a TextFileReader.
*/
public TextFileReader() {
reset();
}

/**
* Resets all the variables in this bean.
*/
public void reset() {
fileName = "";
errorMessage = "";
columns = 0;
rowCount = 0;
}

/**
* Sets the error message, if an error occurs.
*/
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

/**
* Returns the error message, if any.
*/
public String getErrorMessage() {
return errorMessage;
}

/**
* Returns the filename.
*/
public String getFileName() {
return fileName;
}

/**
* Sets the filename.
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}

/**
* Returns the amount of rows in the file.
*/
public int getRows() {
return rowCount;
}

/**
* Returns the maximum amount of columns in a row.
*/
public int getColumns() {
return columns;
}

/**
* Returns the content of the file in a String.
* If an error occurs, like if the file does not exists, null is returned.
*/
public String getContent() {
String content = "";
File file = new File(fileName);
if (!file.exists()) {
setErrorMessage("Error: The file '" + fileName + "' does not exists.");
return null;
}
else if (file != null) {
try {
// Create an BufferedReader so we can read a line at the time.
BufferedReader reader = new BufferedReader(new FileReader(file));
String inLine = reader.readLine();
while (inLine != null) {
if (inLine.length() + 1 > columns)
columns = inLine.length() + 1;
content += (inLine + System.getProperty("line.separator"));
inLine = reader.readLine();
rowCount++;
}
return content;
}
catch (IOException e) {
setErrorMessage("Error reading the file: " + e.getMessage());
return null;
}
}
else {
setErrorMessage("Unknown error!");
return null;
}
}
}



(原文来自jsp-interest.com)
  
JSP虚拟主机 | JSP空间 | JSP主机 | JSP双线虚拟主机 | JAVA空间 | JAVA虚拟主机 | 虚拟服务器 | JSP 虚拟服务器 | VPS
Virtual Private Server | JAVA虚拟服务器 | VM服务器 | VHOST | 虚拟操作系统 | JSP论坛 | JAVA论坛 | JSP站点论坛
Copyright © 2002-2005 JSPCN.net. All rights reserved.
JSP中文网    备案序号:蜀ICP备05001583号
成都恒海科技发展有限公司    成都市一环路南二段6号新瑞楼三楼8号