**************JSP代码: TextFileReader.jsp
<!--
TextFileReader.jsp
Written by Martin Lindahl
Copyright 1999, w3it.com, distributed by JSPea
--><%@ page import = "textfileaccess.TextFileReader" %><jsp:useBean id="file_reader" class="textfileaccess.TextFileReader" scope="session"/>
<jsp:setProperty name="file_reader" property="FileName"/><html>
<head><title>Read a text file</title></head>
<body bgcolor="white">
<font size=4><% if (file_reader.getFileName() != "") { %>The content of the file '<% out.println(file_reader.getFileName()); %>' : <br><br><% if (file_reader.getContent() != null) { %><Form>
<TEXTAREA rows=<%= file_reader.getRows() %> cols=<%= file_reader.getColumns() %> id=textarea1 name=textarea1><% out.println(file_reader.getContent()); %></TEXTAREA>
</Form><% } else { %>
<% out.println(file_reader.getErrorMessage()); %><% } %><br><br><% file_reader.reset(); %>
Do you want to <a href="TextFileReader.jsp">look at another file</a>?
<% } else { %>Welcome to the 'Read a file in JSP' example.<br>
The example simply shows the file in a textarea.<p>
Please fill out what file you want to look at. Be sure to type the complete path.<p><form method=get>
FileName? <input type=text name=FileName>
<input type=submit value="Show it!">
</form><% } %></font>
</body>
</html>
**************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;
}
}
}
<!--
TextFileReader.jsp
Written by Martin Lindahl
Copyright 1999, w3it.com, distributed by JSPea
--><%@ page import = "textfileaccess.TextFileReader" %><jsp:useBean id="file_reader" class="textfileaccess.TextFileReader" scope="session"/>
<jsp:setProperty name="file_reader" property="FileName"/><html>
<head><title>Read a text file</title></head>
<body bgcolor="white">
<font size=4><% if (file_reader.getFileName() != "") { %>The content of the file '<% out.println(file_reader.getFileName()); %>' : <br><br><% if (file_reader.getContent() != null) { %><Form>
<TEXTAREA rows=<%= file_reader.getRows() %> cols=<%= file_reader.getColumns() %> id=textarea1 name=textarea1><% out.println(file_reader.getContent()); %></TEXTAREA>
</Form><% } else { %>
<% out.println(file_reader.getErrorMessage()); %><% } %><br><br><% file_reader.reset(); %>
Do you want to <a href="TextFileReader.jsp">look at another file</a>?
<% } else { %>Welcome to the 'Read a file in JSP' example.<br>
The example simply shows the file in a textarea.<p>
Please fill out what file you want to look at. Be sure to type the complete path.<p><form method=get>
FileName? <input type=text name=FileName>
<input type=submit value="Show it!">
</form><% } %></font>
</body>
</html>
**************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;
}
}
}
解决方案 »
- 求助:java.sql.SQLException: Already closed总是自动关闭(奇怪)
- 由于时间紧迫,需要一个javamail的实用类,
- 求大神帮忙!!!! java tomcat配置
- web.xml是做什么用的,想听高手详解!
- 用JDBC连sql server2000的问题
- 怎么设ftp啊
- 求助:关于EasyUI CRUD中的“删除一条数据不刷新,但数据库中已删除”解决方案
- 域名问题,高手请进
- 哪位好心的大侠能教我一下如何在Tomcat4.0.6中部署Struts呢?100分
- JSP+ORCALE8的多国语言的转换问题!
- jspsmartupload上载到虚拟路径的问题?
- 菜鸟问题,如何得到本网页的url?
<%@ page contentType="text/html" %>
<%
File my_File=new File("d:/jbuilder5/jakarta-tomcat-3.2.1/webapps/examples/test.csv");
try{
if (!my_File.canWrite())
{
my_File.createNewFile();
}
FileWriter w=new FileWriter(my_File);
String name=new String(request.getParameter("name").getBytes("ISO8859_1"),"gb2312");
String pass=new String(request.getParameter("pass").getBytes("ISO8859_1"),"gb2312");
String t1=new String("姓名".getBytes("ISO8859_1"),"gb2312");
t1+=",";
String t2=new String("密码 ".getBytes("ISO8859_1"),"gb2312");
t2+="\n";
w.write(t1);
w.write(t2);
w.write(name+",");
w.write(pass+"\n");
w.flush();
response.sendRedirect("./test.csv");
}catch(Exception e)
{
out.print(e);
}%>