目录
FileWriter:以下两种构造,都可以重载,指定一个boolean 类型的参数,用来指定追加还是覆盖文件内容
字节流
字节输入流(读)
InputStream
read()
read(byte[] c)
read(byte[],int off,int len);
close();
FileInputStream
new FileInputStream(File file)
new FileInputStream(String path)
close();
字节输出流( 写 )
OutputStream
write()
write(byte[] c)
write(byte[],int off,int len)
close();
flush():强制将缓冲区清空
FileOutputStream
new FileOutputStream(File file)
new FileOutputStream(String path)
new FileOutputStream(String path,boolean append);可以指定覆盖或者追加文件内容
字符流
字符输入流(读)
Reader
read()
read(char[] c)
read(char[] c,int off,int len)
close()
InputStreamReader
new InputStreamReader(InputStream inputStream)
new InputStreamReader(InputStream) inputStream,String name)//参数1 字节流,参数二,指定编码格式名
FileReader
new FileReader(File file)
new FileReader(String path)
中文乱码原因
原因:文件编码格式 和 程序换环境的编码格式不一致
解决办法:
字符流去读的时候,指定字符流的编码格式
FileReader 无法指定编码格式,会按照文件系统默认编码格式
使用
InputStreamReader(InputStream inputStream,String name)
字符输出流 (写)
Writer
write(String string);
write(String string,int off,int len)
close()
flush()
OutputStreamReader:可以指定字符编码格式
new OutputStreamReader(InputStream inputStream)
new OutputStreamReader(InputStream) inputStream,String name)//参数1 字节流,参数二,指定编码格式名
FileWriter:以下两种构造,都可以重载,指定一个boolean 类型的参数,用来指定追加还是覆盖文件内容
new FileWriter(File)
new FileWriter(String path)
缓冲流
BufferedReader
readLine();
new BufferedReader(Reader reader);
BufferedWriter: 将数据缓冲到字符输出流
new BufefredWriter(Writer writer);
二进制文件的读写:
DataInputStream:
DataOutputStream:
对象流读写对象信息
序列化:ObjectOnputStream
writeObjrct(Object)
反序列化:ObjectInputStream
readObject(),返回的是Object对象,一般都需要强转类型
常见异常:
NotSerizableException:类没有实现Serizable接口,不可序列化