第六章 JSP 文件操作 ( 续 )

16
第第第 JSP 第第第第 ( 第 ) 南南南南南南南南南南南南南 西 南南南南南南JSP

description

西南科技大学网络教育系列课程 动态网页设计( JSP ). 第六章 JSP 文件操作 ( 续 ). 字符流. 在实际应用中,存在一类文本数据,它们可能采用各种不同的字符编码方式 ( 字符集 ) ,可能是单字节字符,也可能是多字节字符,这就需要借助于字符流来处理文本类信息。读取器 (Reader) 和写出器 (Writer) 是所有字符流的超类,它们是直接继承 Object 类的抽象类。. 字符输入流的类层次结构. 字符输入流. 常用方法:. public abstract void close() throws IOException. - PowerPoint PPT Presentation

Transcript of 第六章 JSP 文件操作 ( 续 )

Page 1: 第六章  JSP 文件操作 ( 续 )

第六章 JSP文件操作 (续 )

西南科技大学网络教育系列课程动态网页设计( JSP)

Page 2: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

字符流 在实际应用中,存在一类文本数据,它们可能采用各种不同的字符编码方式 ( 字符集 ) ,可能是单字节字符,也可能是多字节字符,这就需要借助于字符流来处理文本类信息。读取器 (Reader) 和写出器 (Writer) 是所有字符流的超类,它们是直接继承 Object 类的抽象类。

字符输入流的类层次结构

Page 3: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

字符输入流常用方法:

public abstract void close() throws IOException

public int read() throws IOException

public int read(char[] cbuf) throws IOException

public abstract int read(char[] cbuf, int off, int len) throws IOException

public long skip(long n) throws IOException

Page 4: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

字符输入流FileReader 类

public FileReader(String fileName) throws FileNotFoundException

public FileReader(File file) throws FileNotFoundException

实例:用字符输入流读取文件 fileReaderExample1.jsp

Page 5: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

字符流

字符输出流的类层次结构

Page 6: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

字符输出流Writer 类常用方法

public void write(String str) throws IOExceptionpublic void write(char[] cbuf) throws IOExceptionpublic abstract void write(char[] cbuf, int off, int len)throws IOException

public abstract void close() throws IOException

public abstract void flush() throws IOException

Page 7: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

字符输出流FileWriter 类

public FileWriter(String fileName) throws IOExceptionpublic FileWriter(File file) throws IOExceptionpublic FileWriter(String fileName, boolean append) throws IOException

public BufferedWriter(Writer out) public BufferedWriter(Writer out, int sz)

FileWriter 类

实例:用字符输出流写文件 filewriterExample1.jsp

Page 8: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

6.4 随机读写文件RandomAccessFile 和 FileInputStream 和 FileOutputStream 不同,它既可以读取一个文件,也可以写入一个文件。 RandomAccessFile 类具有如下的构建器:

public RandomAccessFile(String name, String mode) throws FileNotFoundException

public RandomAccessFile(File file, String mode) throws FileNotFoundException

其中第一个参数使用文件名或 File 对象指定一个文件。第二个参数 mode 指定文件的打开方式, mode 为“ r” 时,指定文件按只读方式打开; mode 为“ rw” 时,文件按只读写方式打开。

Page 9: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

RandomAccessFile 的方法public void close() throws IOException

public long getFilePointer() throws IOException

public long length() throws IOException

public int read() throws IOException

public final void readFully(byte[] b) throws IOException

public final String readLine() throws IOException

Page 10: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

RandomAccessFile 的方法public void seek(long pos) throws IOException

public void setLength(long newLength) throws IOException

public void write(byte[] b) throws IOException

public final void writeByte(int v) throws IOException

程序实例:随机读取文件程序示例 RandomAccessFileExample1.jsp

程序实例:故事接龙 InputStory.jsp

Page 11: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

文件的上传与下载用普通方法实现文件上传

实例:文件上传UploadFileExample1.jsp

AccepteUploadFile.jsp

实例:文件下载 DownFileExample2.jsp

Page 12: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

用组件实现文件的上传与下载jspSmartUpload

组件安装相关类的介绍

File 类savaAs() 方法

savaAs(String dstFileName)savaAs(String dstFileName,int savaAsOption)

SAVEAS_PHYSICALSAVEAS_VIRTUAL

Page 13: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

jspSmartUpload 相关类的介绍File 类

isMissing()getFieldName()getFileName()

Files 类getCount()getSize()getCollection()

Page 14: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

jspSmartUpload 相关类的介绍request 类smartUpload 类

Initialize()upload()save()getRequest()getFiles()setAllowedFileList(String allowedFilesList)

Page 15: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

jspSmartUpload 相关类的介绍smartUpload 类

setDeniedFilesList()setMaxFileSize()setTotalMaxFileSize()downloadFile()实例:用 jspSmartUpload 组件实现文件上传uploadFile.html do_upload.jsp实例:应用 jspSmartUpload 组件进行较复杂的表单处理uploadFile2.html do_upload2.jsp

Page 16: 第六章  JSP 文件操作 ( 续 )

动态网页设计( JSP) http://www.swust.net.cn

文件的分页显示实例:分页读取文件内容 fenye.jsp

小结