?? 第6章(2).txt
字號:
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);
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 + -