在JSP中,如何打开或关闭一个文件或读取一个文件的内容?(100分)

  • 主题发起人 chenwei_oracle
  • 开始时间
C

chenwei_oracle

Unregistered / Unconfirmed
GUEST, unregistred user!
在JSP中,如何打开或关闭一个文件或读取一个文件的内容?
 
FileSystemObject
Provides access to a computer's file system.
y = new ActiveXObject("Scripting.FileSystemObject")
Remarks
The following code illustrates how the FileSystemObject is used to return a TextStream object that can be read from or written to:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c://testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
In the example code, the ActiveXObject object is assigned to the FileSystemObject (fso). The CreateTextFile method then
creates the file as a TextStream object (a), and the WriteLine method writes a line of text to the created text file. The Close method flushes the buffer and closes the file.
 
楼上的兄弟好像是理解错了题意,脚本语言似乎不是问题的所在,
对于JSP,还是来BEAN的封装好一点!
这里给出读写文件的BEAN,至于调用,就视情况自己搞定了,^_^
读文件Bean:
import java.io.*;
import java.awt.event.*;
import java.util.*;
/**
* 该文件实现的Bean类是一个提供基本的读取文件功能的Bean类
*/
public class fileReader {
private String fileName;
private String errorMessage;
private int columns;
private int rowCount;
/**
* fileReader的构造函数
*/
public fileReader() {
reset();
}
/**
* 重置所有的变量
*/
public void reset() {
fileName = "";
errorMessage = "";
columns = 0;
rowCount = 0;
}
/**
* 设置错误信息
*/
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
/**
* 返回错误信息
*/
public String getErrorMessage() {
return errorMessage;
}
/**
* 返回文件名
*/
public String getFileName() {
return fileName;
}
/**
* 设置文件名
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* 返回文件内容的行数
*/
public int getRows() {
return rowCount;
}
/**
* 返回文件中最大一行的列数
*/
public int getColumns() {
return columns;
}
/**
* 将文件内容返回到一个字符串中,
* 如果存在错误信息,则返回null值
*/
public String getContent() {
String content = "";
File file = new File(fileName);
if (!file.exists()) {
setErrorMessage("Error: The file '" + fileName + "'do
es not exists.");
return null;
}
else
if (file != null) {
try {
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;
}
}
}
写文件Bean:
import java.io.*;
public class fileWriter
{
//文件路径
private String filePath;
//写入的字符串
private String str;
//初始化
public fileWriter()
{
filePath = null;
str = "This is a string";
}
//设置文件路径
public void setFilePath(String filePath)
{
this.filePath = filePath;
}
//得到文件路径
public String getFilePath()
{
return filePath;
}
//得到字符串
public void setStr(String str)
{
this.str = str;
}
//设置字符串
public String getStr()
{
return str;
}
//写入字符串到文件中,成功则返回ok字符串
public String writeStr()
{
try
{
File f = new File(filePath);
PrintWriter out = new PrintWriter(new FileWriter(f));
out.print(this.getStr() + "/n");
out.close();
return "ok";
}
catch (IOException e)
{
return e.toString();
}
}
}
 
谢谢网友们的关心和支持,特别是jsxjd,你回答的问题很经典,你在JAVABEAN上有很深的造诣
,在此深感表谢。我的E-MAIL是tarim@vip.sina.com,我叫陈伟,是新疆塔里木油田的一名
软件开发人员,如果有时间来新疆的话,我带你去旅游,带你去看看大漠风光,本人在此希望和
你结识成朋友,望能联系。
 
to chenwei_oracle:
faint...老兄,你是不是搞错了呀...
jsxjd是用ActiveX对象实现的,我用的才是JAVA BEAN。
 
fishapple兄弟:
sorry,我把人搞错了,给你的分打少了。现在只能说声抱歉,下次一定给你的分高高的。
其实你的答案才是对的。
 
to chenwei_oracle:
没有关系了,我还以为是自己错了呢。反正大家都是交流经验而已,分数是次要的
关键是是否能够解决问题,希望以后可以大家互相进步。
 
顶部