📄
字号:
ReadFile.java:
import java.io.*;
public class ReadFile
{ String filePath="c:/",fileName="";
//设置目录属性的值:
public void setFilePath(String s)
{filePath=s;
try{byte b[]=filePath.getBytes("ISO-8859-1");
filePath= new String(b);
}
catch(Exception ee)
{ }
}
// 设置文件名字属性的值:
public String getFilePath()
{return filePath;
}
public void setFileName(String s)
{ fileName=s;
try{byte b[]=fileName.getBytes("ISO-8859-1");
fileName=new String(b);
}
catch(Exception ee)
{ }
}
public String getFileName()
{return fileName;
}
//列出目录中的文件:
public String[] listFile()
{ File dir=new File(filePath);
String file_name[]=dir.list();
return file_name;
}
//读取文件的原始信息:
public StringBuffer readFile()
{ try{ File file=new File(filePath,fileName);
FileReader in=new FileReader(file) ;
PushbackReader push=new PushbackReader(in);
StringBuffer stringbuffer=new StringBuffer();
int c;
char b[]=new char[1];
while ( (c=push.read(b,0,1))!=-1)//读取1个字符放入字符数组b。
{ String s=new String(b);
if(s.equals("<")) //回压的条件
{ push.unread('&');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('L');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('T');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
}
else if(s.equals(">")) //回压的条件
{ push.unread('&');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('G');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('T');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
}
else if(s.equals("\n"))
{ stringbuffer.append("<BR>");
}
else
{ stringbuffer.append(s);
}
}
push.close();
in.close();
return stringbuffer;
}
catch(IOException e)
{return new StringBuffer("不能读取文件");
}
}
}
选择目录的页面(效果如图6.23所示)
filepathselect.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.util.*" %>
<%@ page import="ReadFile" %>
<HTML>
<BODY ><Font size=1>
<P>请选择一个目录:
<FORM action="listfilename.jsp" method=post>
<Select name="filePath" >
<Option value="f:/2000"> f:/2000
<Option value="d:/tomcat">D:/tomcat
<Option value="d:/tomcat/jakarta-tomcat-4.0/webapps/root">Root
<Option value="F:/javabook">f:/javabook
<Option value="f:/Example">f:/Example
</Select>
<Input type=submit value="提交">
</FORM>
</FONT>
</BODY>
</HTML>
选择文件页面(效果如图6.24所示)
listfilename.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="ReadFile" %>
<HTML>
<BODY ><Font size=2>
<jsp:useBean id="file" class="ReadFile" scope="session" >
</jsp:useBean>
<jsp:setProperty name= "file" property="filePath" param="filePath" />
<P>该目录
<jsp:getProperty name= "file" property="filePath" />
有如下文件:<BR>
<% String name[]=file.listFile();
for(int i=0;i<name.length;i++)
{out.print("<BR>"+name[i]);
}
%>
<Form action=readfile.jsp method="post">
<P>输入文件的名字:
<Input type=text name="fileName" name= "f" >
<Input type=submit value="提交">
</Form>
<BR>
<FORM action="filepathselect.jsp" method=post name=form>
<Input type=submit value="重新选择目录">
</FORM>
</Body>
</HTML>
读取文件内容页面(效果如图6.25所示)
readfile.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="ReadFile" %>
<HTML>
<BODY ><Font size=2>
<jsp:useBean id="file" class="ReadFile" scope="session" >
</jsp:useBean>
<jsp:setProperty name= "file" property="fileName" param="fileName" />
<P>文件
<jsp:getProperty name= "file" property="fileName" />
的内容如下:<BR>
</Font>
<Font size=1>
<% StringBuffer s=file.readFile();
out.print(s);
%>
<FORM action="filepathselect.jsp" method=post name=form>
<Input type=submit value="重新选择目录">
</FORM>
<BR>
<FORM action="listfilename.jsp" method=post name=form>
<Input type=submit value="重新选择文件">
</FORM>
</Body>
</HTML>
WriterFile.java:
import java.io.*;
public class WriterFile
{ String filePath=null,
fileName=null,
fileContent=null;
public WriterFile()
{ filePath="C:/";
fileName="无标题";
fileContent="无内容";
}
public void setFilePath(String s)
{filePath=s;
try{byte b[]=filePath.getBytes("ISO-8859-1");
filePath= new String(b);
}
catch(Exception ee)
{ }
}
public String getFilePath()
{return filePath;
}
public void setFileName(String s)
{ fileName=s;
try{byte b[]=fileName.getBytes("ISO-8859-1");
fileName=new String(b);
}
catch(Exception ee)
{ }
}
public String getFileName()
{return fileName;
}
//获取属性fileContent的值,为了能显示HTML或JSP源文件,需进行流的处理技术:
public String getFileContent()
{ try{ StringReader in=new StringReader(fileContent) ;//指向字符串的字符流。
PushbackReader push=new PushbackReader(in);
StringBuffer stringbuffer=new StringBuffer();
int c;
char b[]=new char[1];
while ( (c=push.read(b,0,1))!=-1)//读取1个字符放入字符数组b。
{ String s=new String(b);
if(s.equals("<")) //回压的条件
{ push.unread('&');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('L');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('T');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
}
else if(s.equals(">")) //回压的条件
{ push.unread('&');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('G');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('T');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
}
else if(s.equals("\n"))
{ stringbuffer.append("<BR>");
}
else
{ stringbuffer.append(s);
}
}
push.close();
in.close();
return fileContent=new String(stringbuffer);
}
catch(IOException e)
{return fileContent=new String("不能读取内容");
}
}
//写文件:
public void setFileContent(String s)
{ fileContent=s;
try{
byte b[]=fileContent.getBytes("ISO-8859-1");
fileContent=new String(b);
File file=new File(filePath,fileName);
FileWriter in=new FileWriter(file) ;
BufferedWriter buffer=new BufferedWriter(in);
buffer.write(fileContent);
buffer.flush();
buffer.close();
in.close();
}
catch(Exception e)
{}
}
}
提交文件内容的页面(包括文件所在目录、文件名及内容。效果如图6.26所示)
writeContent.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="ReadFile" %>
<HTML>
<BODY ><Font size=1>
<P>请选择一个目录:
<FORM action="writeFile.jsp" method=post>
<Select name="filePath" >
<Option value="f:/2000"> f:/2000
<Option value="d:/tomcat">D:/tomcat
<Option value="d:/root">Root
<Option value="F:/javabook">f:/javabook
<Option value="f:/Example">f:/Example
</Select>
<P>输入保存文件的名字:
<Input type=text name="fileName" >
<P>输入文件的内容:
<BR>
<TextArea name= "fileContent" Rows= "10" Cols= "40" >
</TextArea>
<BR><Input type=submit value="提交">
</FORM>
</FONT>
</BODY>
</HTML>
将内容写入文件的页面(效果如图6.27所示)
writeFile.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="WriterFile" %>
<HTML>
<BODY ><Font size=1>
<jsp:useBean id="file" class="WriterFile" scope="page" >
</jsp:useBean>
<jsp:setProperty name= "file" property="filePath" param="filePath" />
<jsp:setProperty name= "file" property="fileName" param="fileName" />
<jsp:setProperty name= "file" property="fileContent" param="fileContent" />
<BR>你写文件到目录:
<jsp:getProperty name= "file" property="filePath" />
<BR>文件的名字是:
<jsp:getProperty name= "file" property="fileName" />
<BR>文件的内容是:
<jsp:getProperty name= "file" property="fileContent" />
</Font>
</Body>
</HTML>
DataBaseInquire.java:
import java.sql.*;
public class DataBaseInquire
{ String keyword;
public DataBaseInquire()
{keyword="";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -