文件流 Java I/O 和反射
什么是文件:
相关记录或放在一起的数据的集合
Java程序如何访问文件属性:
JAVAAPI:java.io.File
File类访问文件属性:图
File类的常用方法:图
createNewFile();指创建文件不创建文件夹。
流
通过流来读写文件:
流是一组有序的数据序列
以先进先出方式发送信息的通道。
输入流:inputStream和Writer(来自数据源的数据流)
输出流:Outputstream和Reader(流向目的地的数据流)
源数据源 information—读—>程序
程序—写—>information 目标数据源
Java流的分类:图
--字节流--
InputStream:字节输入流 (读)//*抽象类*
InputStream类的常用方法:
int read():
从输入流一个字节一个字节的读,返回的是该字节的整数表示形式,如果到了输入
流的末尾,返回-1
int read(byte[] b): //字节数组
从输入流读取若干字节,把这些字节保存到数组b中,返回的是读取到的字节数,如果
到了输入流的末尾,返回-1
int read(byte[] b,int off,int len):
从输入流读取若干字节,把这些字节保存到数组b中,off指的是字节数组中开始保存数据
的起始下标,len指读取的字节数目。返回的是读取到的字节数,如果到了输入流的末尾,返回-1
子类File InputStream常用的构造方法
FileInputStream常用的构造方法 //*子类*
new FileInputStream(File file)
new FileInputStream(String path)
OutputStream:字节输出流(写)//*抽象类*
OutputStream类的常用方法
write(int c):
将制定的字节数据写入该输出流。
write(byte[]):
将数组中的所有字节写入此输出流中
write(byte[],int off,int len):
从off开始将数组中到len的字节数据输出到输出流中
close()
flush():强制将缓冲区清空
FileOutputStream常用的构造方法 //*子类*
new FileOutputStream(File file)
new FileOutputStream(String path)
new FileOutputStream(String path,boolean append)://可以指定覆盖或追加文件内容
void close():关掉流
int available():可以从输入流中读取的字节数目
使用FileInputStream读取文本文件 实现步骤:
引入相关的类-->构造文件输入流FileInputStream对象-->读取文本文件的数据-->关闭文件流对象
----字符流---
使用BufferedReader和FileReader读文本文件。
BufferedReader和FileReader 两个类都是Reader类抽象类的子类,它们可以通过字符流
的方式读取文件,并使用缓冲区,提高了读文本文件的效率。
UTF-8(国际通用编码格式) ANSI GBK GB2312中文编码
字符输入流(读):Reader //父类
Reader类常用方法:
int read()
int read(char[] c)
read(char[] c,int off,int len)
void close()
InputStreamReader:可以指定字符编码格式(解决中文乱码(字节流-字符流)) //子类
InputStreamReader(InputStream in)
InputStreamReader(InputStream in,String charSetName)
//从形式上可看出,它是字符流,但他是把字节流包成字符流 同时包的时候
//还可以指定编码格式。(其实自身用的是适配器的模式)
*FileReader() //孙子类
new FileReader(File file)
new FileReader(String path)
System.out.println(System.getProperty("file.encoding"));
显示系统默认编码格式
常见问题:中文乱码
原因:文件编码格式 和 程序环境的编码格式不一样
解决方案:字符流去读的时候,指定字符流的编码格式
FileReader 无法指定编码格式,会按照文件系统默认编码格式读
System.out.println(System.getProperty("file.encoding"));
所以用InputStreamReader
缓冲流
BufferedReader 是FileReader的子类
带缓冲区的流 将文件所有数据先读到缓冲区
readLine()读取一行数据
字符输出流(写):
Writer
Write(String)
close()
flush():请空缓存
OutputStreamWriter:可以指定字符编码格式
new OutputStreamWriter:(OutputStream)
new OutputStreamWriter:(OutputStream,String charSetName)
FileWriter:以下两种构造,都可以重载,指定一个boolean类型的参数,用来指定
追加还是覆盖文件内容
new FileWriter(File file)
new FileWriter(String path)
BufferedWriter:带缓冲区的输出流
BufferedWriter类是Writer类的子类
BufferedWriter常用的构造方法
BufferedWriter(Writer put)
读写二进制文件(使用字节流读写)
读二进制文件
构建一个DataInputStream.包装FileInputStream
FileInputStream f1=new FileInputStream();
DataInputStream d1=new DataInputStream();
d1.read();
写二进制文件
构建一个DataOutputStream.类包装FileOutputStream
FileOutputStream f1=new FileOutStream();
DataOutStream d1=new DataOutputStream();
d1.write();
序列化和反序列化
序列化是将对象的状态写入到特定的流中 的过程
反序列化则是从特定的流中获取数据重新构建对象的过程
对象 -- 流
实现过程
实现serializable接口(可以拥有序列化的能力)
创建对象输出流
调用writeObject()方法将对象写入文件
关闭对象输出流
ObjectInputStream 反序列化 .readObject();
ObjectOutputStream 序列化 writeObject(Object)
常见异常
NotSerializableException 没有实现SerializableException接口,没有被序列化的能力
如果想要某一个对象状态不被序列化 则在相应属性前面加关键字 transient
(屏蔽敏感字段)
Arithmetic