jsp程序设计期末复习整理

2026/4/27 21:28:08

File对象调用方法 publicboolean delete();

可以删除当前对象代表的文件或目录。如果File对象表示的是一个目录,则该目录必须是一个空目录,删除成功将返回true

使用字节流读写文件

java.io包提供大量的流类。称InputStream类及其子类对象为字节输入流类,称OutputStream类及其子类对象为字节输出流。

InputStream类的常用方法:

21 / 37

? int read()输入流调用该方法从源中读取单个字节的数据,该方法返回字节值(0~255之间的一个整数)。

如果未读出字节就返回-1。

? int read(byte b[])输入流调用该方法从源中试图读取b.length个字节到字节数组b中,返回实际读取的字节

数目。如果到达文件的末尾,则返回-1。

? int read(byte b[], int off, intlen)输入流调用该方法从源中试图读取len个字节到字节数组b中,并返回实际

读取的字节数目。如果到达文件的末尾,则返回-1。参数off指定从字节数组的某个位置开始存放读取的数据。

? void close()输入流调用该方法关闭输入流。

? long skip(long numBytes)输入流调用该方法跳过numBytes个字节,并返回实际跳过的字节数目。 OutputStream类的常用方法:

? void write(int n)输出流调用该方法向输出流写入单个字节。

? void write(byte b[])输出流调用该方法向输出流写入一个字节数组。

? void write(byte b[],intoff,intlen)从给定字节数组中起始于偏移量off处取len个字节写入到输出流。? void close()关闭输出流。 输入、输出流示意图

? FileInputStream类是InputStream的子类。 构造方法: FileInputStream(String name) FileInputStream(File file)

使用构造方法可能发生IOException异常。输入流通过调用read方法读出源中的数据。

? FileOutputStream是OutputStream类的子类。 构造方法: FileOutputStream(String name) FileOutputStream(File file)

使用构造方法可能发生IOException异常。输出流通过调用write方法把字节写入到目的地。

22 / 37

读取一个名为myfile.dat的文件

FileInputStream流经常和BufferedInputStream流配合使用,

FileOutputStream流经常和BufferedOutputStream流配合使用类配合使用提高读写效率。

? BufferedInputStream类的一个常用的构造方法是: BufferedInputStream(InputStream in); 读取文件A.txt常用下列方式:

FileInputStream in=new FileInputStream(\

BufferedInputStreambufferRead=new BufferedInputStream(in);

? BufferedOutputStream类的一个常用的构造方法是: BufferedOutputStream(OutputStream out); 向文件B.txt写入字节常用下列方式:

FileOutputStream out=new FileOutputStream(\

BufferedOutputStreambufferWriter=new BufferedOutputStream(out); 例5-6

使用字符流读写文件

字节流不能直接操作Unicode字符,所以Java提供了字符流。由于汉字在文件中占用2个字节,如果使用字节流,读取不当会出现乱码现象,采用字符流就可以避免这个现象。在Unicode字符中,一个汉字被看做一个字符。 所有字符输入流类都是Reader(输入流)抽象类的子类。 所有字符输出流都是Writer(输出流)抽象类的子类。 Reader类中常用方法:

? int read()

23 / 37

? int read(char b[])

? int read(char b[], int off, intlen) ? void close()

? long skip(long numBytes)

Writer类中常用方法:

? void write(int n)输出流写入一个字符。

? void write(char b[])向输出流写入一个字符数组。 ? void write(char b[],intoff,int length) ? void close() 关闭输出流。

? FileReader和FileWriter类是Reader和Writer的子类。 ? FileReader构造方法:

? FileReader(String filename) ? FileReader(File file) ? FileWriter构造方法:

? FileWriter(String filename) ? FileWriter (File file)

FileReader流经常和BufferedReader流配合使用; FileWriter流经常和BufferedWriter流配合使用。

? BufferedReader流还可以使用方法 String readLine()读取一行;

? BufferedWriter流还可以使用方法

void write(String s,intoff,int length)将字符串s的一部分写入文件. newLine() 向文件写入一个行分隔符

24 / 37


jsp程序设计期末复习整理.doc 将本文的Word文档下载到电脑
搜索更多关于: jsp程序设计期末复习整理 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219