寻求JSP文件操作的简单例子

ragweed_wg 2002-02-26 09:20:33
寻求一个JSP对文件读写的简单的原码,谢谢。
...全文
4 点赞 收藏 2
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengji 2002-02-26
<%@ page import="java.io.*,java.lang.Exception;"%>
<%@ 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);
}

%>
回复
zfluo 2002-02-26
**************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;
}
}
}

回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告