160 likes | 335 Views
西å—科技大å¦ç½‘络教育系列课程 动æ€ç½‘页设计( JSP ). 第å…ç« JSP 文件æ“作 ( ç» ). å—符æµ. 在实际应用ä¸ï¼Œå˜åœ¨ä¸€ç±»æ–‡æœ¬æ•°æ®ï¼Œå®ƒä»¬å¯èƒ½é‡‡ç”¨å„ç§ä¸åŒçš„å—ç¬¦ç¼–ç æ–¹å¼ ( å—符集 ) ,å¯èƒ½æ˜¯å•å—节å—符,也å¯èƒ½æ˜¯å¤šå—节å—符,这就需è¦å€ŸåŠ©äºŽå—ç¬¦æµæ¥å¤„ç†æ–‡æœ¬ç±»ä¿¡æ¯ã€‚读å–器 (Reader) 和写出器 (Writer) 是所有å—符æµçš„超类,它们是直接继承 Object 类的抽象类。. å—符输入æµçš„类层次结构. å—符输入æµ. 常用方法:. public abstract void close() throws IOException.
E N D
西南科技大学网络教育系列课程 动态网页设计(JSP) 第六章 JSP文件操作(续)
字符流 • 在实际应用中,存在一类文本数据,它们可能采用各种不同的字符编码方式(字符集),可能是单字节字符,也可能是多字节字符,这就需要借助于字符流来处理文本类信息。读取器(Reader)和写出器(Writer)是所有字符流的超类,它们是直接继承Object类的抽象类。 字符输入流的类层次结构
字符输入流 • 常用方法: public abstract void close() throws IOException public int read() throws IOException public int read(char[]cbuf) throws IOException public abstract int read(char[]cbuf, intoff, intlen) throws IOException public long skip(longn) throws IOException
字符输入流 FileReader类 public FileReader(StringfileName) throws FileNotFoundException public FileReader(Filefile) throws FileNotFoundException 实例:用字符输入流读取文件 fileReaderExample1.jsp
字符流 字符输出流的类层次结构
字符输出流 • Writer类常用方法 public void write(String str) throws IOException public void write(char[] cbuf) throws IOException public abstract void write(char[] cbuf, int off, int len)throws IOException public abstract void close() throws IOException public abstract void flush() throws IOException
字符输出流 • FileWriter类 public FileWriter(String fileName) throws IOException public FileWriter(File file) throws IOException public FileWriter(StringfileName, booleanappend) throws IOException FileWriter类 public BufferedWriter(Writerout) public BufferedWriter(Writerout, intsz) 实例:用字符输出流写文件 filewriterExample1.jsp
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”时,文件按只读写方式打开。
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 StringreadLine() throws IOException
RandomAccessFile的方法 public void seek(longpos) throws IOException public void setLength(longnewLength) throws IOException public void write(byte[]b) throws IOException public final void writeByte(intv) throws IOException 程序实例:随机读取文件程序示例 RandomAccessFileExample1.jsp 程序实例:故事接龙 InputStory.jsp
文件的上传与下载 用普通方法实现文件上传 实例:文件上传 UploadFileExample1.jsp AccepteUploadFile.jsp 实例:文件下载DownFileExample2.jsp
用组件实现文件的上传与下载 jspSmartUpload 组件安装 相关类的介绍 File类 savaAs()方法 savaAs(String dstFileName) savaAs(String dstFileName,int savaAsOption) SAVEAS_PHYSICAL SAVEAS_VIRTUAL
jspSmartUpload相关类的介绍 File类 isMissing() getFieldName() getFileName() Files类 getCount() getSize() getCollection()
jspSmartUpload相关类的介绍 request类 smartUpload类 Initialize() upload() save() getRequest() getFiles() setAllowedFileList(String allowedFilesList)
jspSmartUpload相关类的介绍 smartUpload类 setDeniedFilesList() setMaxFileSize() setTotalMaxFileSize() downloadFile() 实例:用jspSmartUpload组件实现文件上传 uploadFile.html do_upload.jsp 实例:应用jspSmartUpload组件进行较复杂的表单处理 uploadFile2.html do_upload2.jsp
文件的分页显示 实例:分页读取文件内容 fenye.jsp 小结