Java高级特性-- I/O
1、文件
相关记录或放在一起的数据的集合
存储文件
硬盘、光盘、软盘
Java程序访问文件属性
Java API :java.io.File类
持久化:把文件永久保存起来
File类访问文件属性
2、File类的常用文件
方法名称 | 说明 |
---|---|
boolean exists( ) | 判断文件或目录是否存在 |
boolean isFile( ) | 判断是否是文件 |
boolean isDirectory( ) | 判断是否是目录 |
String getPath( ) | 返回此对象表示的文件的相对路径名 |
String getAbsolutePath( ) | 返回此对象表示的文件的绝对路径名 |
String getName( ) | 返回此队长表示的文件或目录的名称 |
boolean delete( ) | 删除此对象指定的文件或目录 |
boolean createNewFile( ) | 创建名称的空文件,不创建文件夹 |
long length( ) | 返回文件的长度,单位为字节,如果文件不存在则返回OL |
3、流
流是一组有序的数据序列
以先进先出方式发送信息通道
通过流来读写文件
InputStream 来自数据源的数据流
OutputStream 流向目的地的数据流
流的分类
字节流
InputStream类常用方法(抽象类)
方法 | 说明 |
---|---|
(int) read( ) | 从输入流中读取单个字符 |
(int) read(byte[ ] b) | 从输入流中读取c.length长度的字符,保存到字符数组c中,返回实际读取的字符数 |
(int)read(byte[ ] b,int off, int len) | 从输入流中读取最多len的长度字符,保存到字符数组c中,保存的位置从off位置开始,返回实际读取的字符长度 |
(void) close( ) | 关掉流 |
(int) available( ) | 可以从输入流中读取字节数目 |
FileInputStream子类
常用构造方法
FileInputStream(File file)传文件
FileInputStream(String name)传路径
使用FileInputStream 写文本文件
OutputStream类常用方法(抽象类)
方法名称 | 说明 |
---|---|
void write(int c) | 写入一个字节数组 |
void write(byte[ ] buf) | 写入数组buf的所有字节 |
void write(byte[ ] b , int off , int len) | 将字节数组从off位置开始,长度为len的字节数据输出到输出流中 |
void close( ) | 关闭输出流 |
void flush( ) | 强制把缓冲区的数据写到输出流中(自带缓冲空间,写满了再输出,效率更高)不满不输出,需flush( ) |
FileOutputStream子类
常用构造方法
FileOutputStream(File file) 写到哪个文件
FileOutputStream(String name) 写到哪个路径(会覆盖原有数据)
FileOutputStream(String name,boolean append) (boolean append 可以指定覆盖(false)或追加(true))
使用FileOutputStream 读文本文件
字符流
Reader类
常用方法
方法 | 描述 |
---|---|
int read( ) | 如果调用的输入流的下一个字符可读则返回一个整型。遇到文件尾时返回-1 |
int read(char [ ] C) | 试图读取C中的C.length个字符, 返回实际成功读取的字符数。遇到文件尾返回-1 |
int read(char [ ] C,int off,int len) | 试图读取c中从c[off]开始的len个字符,返回实际成功读取的字符数。遇到文件尾返回-1 |
void close( ) | 关闭输入源。进一步的读取将会产生IOException异常 |
子类InputStreamReader类常用构造方法
InputStreamReader(Input)
InputStreamReader(InputStream in,String charsetName)
FileReader类是InputStreamReader的子类
FileReader(File file)
FileReader(String name)
该类只能按照本地平台(比如win10)的字符编码来读取数据,用户不能指定其他的字符编码类型
System.out.println(System.getProperty(“file.encoding”));
获得本地平台的字符编码类型
使用FileReader读取文件
字符流更高效方式:
缓冲流
BufferedReader类
BufferedReader类是Reader的子类
BufferedReader类是带有缓冲区
按行读取内容的readLine( )方法
常用构造方法
BufferedReader(Reader in)
子类BufferedReader特有方法
readLine( )
使用BufferedReader读文本文件
Write类
常用方法
方法 | 描述 |
---|---|
void write(String str) | 向调用的输出流写str |
abstract void write(char [ ]C,int off,int len) | 向 调 用 的 输 出 流 写 入 数 组 C 以 C[off] 为 起 点 的len个字符区域内的内容 |
void close( ) | 关闭输出流。关闭后的写操作会产生IOException异常 |
void flush( ) | 定制输出状态以使每个缓冲器都被清除。也就是刷新输出缓冲 |
子类OutputStreamWriter常用的构造方法
OutputStreamWriter(OutputStream out)
OutputStreamWriter(OutputStream out.String charsetName)
FileWriter类
FileWriter是OutputStreamWrite的子类
new FileWriter(File file)
new FileWriter(String name)
以上两种构造都可以重载,指定一个boolean类型的参数,用来指定追加还是覆盖文件内容
new FileWriter(File file,boolean append)
new FileWriter(String fileName,boolean append)
该类只能按照本地平台的字符编码来写数据,用户不能指定其他的字符编码类型
BufferedWriter类
BufferedWriter类是Writer的子类
带有缓冲区
常用构造方法
BufferedWriter(writer out)
使用BufferedWriter写文件
读写二进制文件
DataInputStream类
DataInputStream类是FileInputStream的子类
与FileInputStream类结合使用读取二进制文件
步骤:
DataOutputStream类
DataOutputStream类是FileOutputStream的子类
与FileOutputStream类结合使用读取二进制文件
步骤:
4、序列化
序列化是将对象的状态写入指定的流中的过程
反序列化时从特定的流中获取数据重新构建对象的过程
序列化的步骤:
常见异常:NotSerializableException类没有实现Serializable接口,没有序列化的能力
transient关键字修饰一条属性
可以在序列化和反序列化的时候屏蔽一些隐瞒属性
反序列化过来该属性为空值(null或0)
反序列化的步骤: