1 缓冲流
缓冲流也称为高效流 、或者高级流 ;之前的字节流可以称为原始流 作用:缓冲流自带缓冲区 、可以提高原始字节流、字符流读写数据的性能
1.2 字节缓冲流
字节缓冲流性能优化原理:
字节缓冲输入流 自带了8KB缓冲池 ,以后我们直接从缓冲池读取数据,所以性能较好字节缓冲输出流 自带了8KB缓冲池 ,数据就直接写入到缓冲池中去,写数据性能极高了
构造器 说明 public BufferedInputStream(InputStream is) 可以把低级的字节输入流包装 成一个高级的缓冲字节输入流管道,从而提高字节输入流读数据的性能 public BufferedOutputStream(OutputStream os) 可以把低级的字节输出流包装 成一个高级的缓冲字节输出流管道,从而提高写数据的性能
功能上并无很大变化,性能提升了
try (
InputStream is = new FileInputStream ( "D:\\resources\\newmeinv.jpeg" ) ;
InputStream bis = new BufferedInputStream ( is) ;
OutputStream os = new FileOutputStream ( "D:\\resources\\newmeinv222.jpeg" ) ;
OutputStream bos = new BufferedOutputStream ( os) ;
) {
byte [ ] buffer = new byte [ 1024 ] ;
int len;
while ( ( len = bis. read ( buffer) ) != - 1 ) {
bos. write ( buffer, 0 , len) ;
}
System . out. println ( "复制完成了!" ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
1.3 字符缓冲流
作用:提高字符输入流读取/写数据的性能,除此之外多了按照行读取数据 的功能
构造器 说明 public BufferedReader(Reader r) 可以把低级的字符输入流包装 成一个高级的缓冲字符输入流管道,从而提高字符输入流读数据的性能 public BufferedWriter(Writer w) 可以把低级的字符输出流包装成一个高级的缓冲字符输出流管道,从而提高字符输出流写数据的性能
字符缓冲输入流新增功能
方法 说明 public String readLine() 读取一行数据返回,如果读取没有完毕,无行可读返回null public void newLine() 换行操作
try (
Reader fr = new FileReader ( "io-app2/src/data01.txt" ) ;
BufferedReader br = new BufferedReader ( fr) ;
) {
String line;
while ( ( line = br. readLine ( ) ) != null ) {
System . out. println ( line) ;
}
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
字符缓冲流自带8K缓冲区 可以提高原始字符流读写数据的性能
2 转换流
作用:防止用UTF-8编码方式 的代码 读取GBK编码方式 的文件 产生乱码
必须文件和代码编码一致才不会乱码
构造器 说明 public InputStreamReader(InputStream is) 可以把原始的字节流 按照代码默认编码转换成字符输入流。几乎不用,与默认的FileReader一样 public InputStreamReader(InputStream is ,String charset) 可以把原始的字节流 按照指定编码转换成字符输入流,这样字符流中的字符就不乱码了(重点 )
输入一定是字节流 InputStream
InputStream is = new FileInputStream ( "D:\\resources\\data.txt" ) ;
Reader isr = new InputStreamReader ( is , "GBK" ) ;
BufferedReader br = new BufferedReader ( isr) ;
String line;
while ( ( line = br. readLine ( ) ) != null ) {
System . out. println ( line) ;
}
如果需要控制写出去的字符使用的编码,怎么办?
可以把字符以指定编码获取字节后再使用字节输出流写出去:
也可以使用字符输出转换流 实现。
构造器 说明 public OutputStreamWriter(OutputStream os) 可以把原始的字节输出流 按照代码默认编码转换成字符输出流。几乎不用 。 public OutputStreamWriter(OutputStream os,String charset) 可以把原始的字节输出流 按照指定编码转换成字符输出流(重点 )
OutputStream os = new FileOutputStream ( "io-app2/src/out03.txt" ) ;
Writer osw = new OutputStreamWriter ( os , "GBK" ) ;
BufferedWriter bw = new BufferedWriter ( osw) ;
bw. write ( "方佬大" ) ;
bw. write ( "方佬贰" ) ;
bw. write ( "方佬叄" ) ;
bw. write ( "方佬肆" ) ;
bw. close ( ) ;
3 序列化对象
3.1 对象序列化
作用:以内存为基准,把内存中的对象存储到磁盘文件 中去,称为对象序列化 。 使用到的流是对象字节输出流:ObjectOutputStream
序列化对象必须实现Serializable序列化接口
public class Student implements Serializable {
. . .
}
构造器 说明 public ObjectOutputStream(OutputStream out) 把低级字节输出流 包装成高级的对象字节输出流
方法名称 说明 public final void writeObject(Object obj) 把对象 写出去到对象序列化流的文件中去
Student s = newStudent ( "方佬肆" , "fanglaosi" , 21 ) ;
ObjectOutputStream oos = new ObjectOutputStream ( new FileOutputStream ( "io-app2/src/obj.txt" ) ) ;
oos. writeObject ( s) ;
oos. close ( ) ;
3.2 对象反序列化
作用:以内存为基准,把存储到磁盘文件中去的对象数据恢复成内存中的对象 ,称为对象反序列化 使用到的流是对象字节输入流:ObjectInputStream
构造器 说明 public ObjectInputStream(InputStream out) 把低级字节输入流 包装成高级的对象字节输入流
方法名称 说明 public Object readObject() 把存储到磁盘文件中去的对象数据恢复成内存中的对象 返回
ObjectInputStream is = new ObjectInputStream ( new FileInputStream ( "io-app2/src/obj.txt" ) ) ;
Student s = ( Student ) is. readObject ( ) ;
System . out. println ( s) ;
4 打印流
作用:打印流可以实现方便、高效的打印数据到文件中去。打印流一般是指:PrintStream ,PrintWriter 两个类 可以实现打印什么数据就是什么数据 ,例如打印整数97写出去就是97,打印boolean的true,写出去就是true
PrintStream和PrintWriter的区别
打印数据功能上是一模一样的,都是使用方便 ,性能高效 (核心优势) PrintStream继承自字节输出流OutputStream ,支持写字节数据 的方法。 PrintWriter继承自字符输出流Writer ,支持写字符数据 出去。
PrintStream
构造器 说明 public PrintStream(OutputStream os) 打印流直接通向字节输出流管道 public PrintStream(File f) 打印流直接通向文件对象 public PrintStream(String filepath) 打印流直接通向文件路径
方法名称 说明 public void print(Xxx xx) 打印任意类型 的数据出去
PrintWriter
构造器 说明 public PrintWriter(OutputStream os) 打印流直接通向字节输出流管道 public PrintWriter (Writer w) 打印流直接通向字符输出流管道 public PrintWriter(File f) 打印流直接通向文件对象 public PrintWriter(String filepath) 打印流直接通向文件路径
方法名称 说明 public void print(Xxx xx) 打印任意类型 的数据出去
PrintWriter ps = new PrintWriter ( "io-app2/src/ps.txt" ) ;
ps. println ( 97 ) ;
ps. println ( 'a' ) ;
ps. println ( 23.3 ) ;
ps. println ( true ) ;
ps. println ( "我是打印流输出的,我是啥就打印啥" ) ;
ps. close ( ) ;
输出语句重定向
System . out. println ( "锦瑟无端五十弦" ) ;
System . out. println ( "一弦一柱思华年" ) ;
PrintStream ps = new PrintStream ( "io-app2/src/log.txt" ) ;
System . setOut ( ps) ;
System . out. println ( "庄生晓梦迷蝴蝶" ) ;
System . out. println ( "望帝春心托杜鹃" ) ;
5 Properties
Properties代表的是一个属性文件,可以把自己对象中的键值对信息 存入到一个属性文件中去 属性文件:后缀是.properties结尾的文件,里面的内容都是 key=value ,后续做系统配置信息的
其实就是一个Map集合,但是我们一般不会当集合使用,因为HashMap更好用
方法名称 说明 void load(Reader reader) 从输入字符流读取属性列表(键和元素对) void store(Writer writer, String comments) 将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流 public Object setProperty(String key, String value) 保存键值对(put) public String getProperty(String key) 使用此属性列表中指定的键搜索属性值 (get) public Set stringPropertyNames() 所有键的名称的集合 (keySet())
5.1 将键值对信息存到配置文件中
Properties properties = new Properties ( ) ;
properties. setProperty ( "方佬大" , "123456" ) ;
properties. setProperty ( "方佬贰" , "123456" ) ;
properties. setProperty ( "方佬叄" , "456789" ) ;
properties. setProperty ( "方佬肆" , "456789" ) ;
System . out. println ( properties) ;
properties. store ( new FileWriter ( "Javacode\\users.properties" ) , "this is users!!" ) ;
5.2 Properties读取属性文件中的键值对信息
Properties properties = new Properties ( ) ;
System . out. println ( properties) ;
properties. load ( new FileReader ( "Javacode\\users.properties" ) ) ;
System . out. println ( properties) ;
String rs = properties. getProperty ( "方佬大" ) ;
System . out. println ( rs) ;
String rs1 = properties. getProperty ( "方佬肆" ) ;
System . out. println ( rs1) ;
String s = properties. stringPropertyNames ( ) ;
6 IO框架:commons-io
commons-io是apache开源基金组织提供的一组有关IO操作的类库,可以提高IO功能开发的效率 commons-io工具包提供了很多有关io操作的类。有两个主要的类FileUtils, IOUtils
导入步骤: Step1:在项目中创建一个文件夹:lib Step2:将commons-io-2.6.jar文件复制到lib文件夹 Step3:在jar文件上点右键,选择 Add as Library -> 点击OK Step4:在类中导包使用
方法名称 说明 String readFileToString(File file, String encoding) 读取文件中的数据, 返回字符串 void copyFileToDirectory(File srcFile, File destDir) 复制文件 void copyDirectoryToDirectory(File srcDir, File destDir) 复制文件夹
FileUtils . copyFileToDirectory ( new File ( "D:\\resources\\hushui.jpeg" ) , new File ( "D:/" ) ) ;
FileUtils . copyDirectoryToDirectory ( new File ( "D:\\resources" ) , new File ( "D:\\new" ) ) ;
FileUtils . deleteDirectory ( new File ( "D:\\new" ) ) ;