缓冲流
也称为高效流、或者高级流。之前学习的字节流可以称为原始流。 作用:缓冲流自带缓冲区、可以提高原始字节流、字符流读写数据的性能 分类:
字节缓冲输入流:BufferedInputStream 字节缓冲输出流:BufferedOutputStream 字符缓冲输入流:BufferedReader 字符缓冲输出流:BufferedWriter
构造器 说明 public BufferedInputStream(InputStream is) 可以把低级的字节输入流包装成一个高级的缓冲字节输入流管道,从而提高字节输入流读数据的性能 public BufferedOutputStream(OutputStream os) 可以把低级的字节输出流包装成一个高级的缓冲字节输出流,从而提高写数据的性能
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) ;
分别使用低级字节流和高级字节缓冲流拷贝大视频,记录耗时。
①使用低级的字节流按照一个一个字节的形式复制文件。 ②使用低级的字节流按照一个一个字节数组的形式复制文件。 ③使用高级的缓冲字节流按照一个一个字节的形式复制文件。 ④使用高级的缓冲字节流按照一个一个字节数组的形式复制文件。
public class ByteBufferTimeDemo {
private static final String SRC_FILE = "D:\\course\\基础加强\\day08-日志框架、阶段项目\\视频\\14、用户购票功能.avi" ;
private static final String DEST_FILE = "D:\\course\\" ;
public static void main ( String [ ] args) {
copy02 ( ) ;
copy04 ( ) ;
}
private static void copy04 ( ) {
long startTime = System . currentTimeMillis ( ) ;
try (
InputStream is = new FileInputStream ( SRC_FILE) ;
InputStream bis = new BufferedInputStream ( is) ;
OutputStream os = new FileOutputStream ( DEST_FILE + "video4.avi" ) ;
OutputStream bos = new BufferedOutputStream ( os) ;
) {
byte [ ] buffer = new byte [ 1024 ] ;
int len;
while ( ( len = bis. read ( buffer) ) != - 1 ) {
bos. write ( buffer, 0 , len) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
long endTime = System . currentTimeMillis ( ) ;
System . out. println ( "使用缓冲的字节流按照一个一个字节数组的形式复制文件耗时:" + ( endTime - startTime) / 1000.0 + "s" ) ;
}
private static void copy03 ( ) {
long startTime = System . currentTimeMillis ( ) ;
try (
InputStream is = new FileInputStream ( SRC_FILE) ;
InputStream bis = new BufferedInputStream ( is) ;
OutputStream os = new FileOutputStream ( DEST_FILE + "video3.avi" ) ;
OutputStream bos = new BufferedOutputStream ( os) ;
) {
int b;
while ( ( b = bis. read ( ) ) != - 1 ) {
bos. write ( b) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
long endTime = System . currentTimeMillis ( ) ;
System . out. println ( "使用缓冲的字节流按照一个一个字节的形式复制文件耗时:" + ( endTime - startTime) / 1000.0 + "s" ) ;
}
private static void copy02 ( ) {
long startTime = System . currentTimeMillis ( ) ;
try (
InputStream is = new FileInputStream ( SRC_FILE) ;
OutputStream os = new FileOutputStream ( DEST_FILE + "video2.avi" )
) {
byte [ ] buffer = new byte [ 1024 ] ;
int len;
while ( ( len = is. read ( buffer) ) != - 1 ) {
os. write ( buffer, 0 , len) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
long endTime = System . currentTimeMillis ( ) ;
System . out. println ( "使用低级的字节流按照一个一个字节数组的形式复制文件耗时:" + ( endTime - startTime) / 1000.0 + "s" ) ;
}
private static void copy01 ( ) {
long startTime = System . currentTimeMillis ( ) ;
try (
InputStream is = new FileInputStream ( SRC_FILE) ;
OutputStream os = new FileOutputStream ( DEST_FILE + "video1.avi" )
) {
int b;
while ( ( b = is. read ( ) ) != - 1 ) {
os. write ( b) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
long endTime = System . currentTimeMillis ( ) ;
System . out. println ( "使用低级的字节流按照一个一个字节的形式复制文件耗时:" + ( endTime - startTime) / 1000.0 + "s" ) ;
}
}
构造器 说明 public BufferedReader(Reader r) 可以把低级的字符输入流包装成一个高级的缓冲字符输入流管道,从而提高字符输入流读数据的性能
方法 说明 public String readLine() 读取一行数据返回,如果读取没有完毕,无行可读返回null
public class BufferedReaderDemo1 {
public static void main ( String [ ] args) {
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 ( ) ;
}
}
}
构造器 说明 public BufferedWriter(Writer w) 可以把低级的字符输出流包装成一个高级的缓冲字符输出流管道,从而提高字符输出流写数据的性能
方法 说明 public void newLine() 换行操作
public class BufferedWriterDemo2 {
public static void main ( String [ ] args) throws Exception {
Writer fw = new FileWriter ( "io-app2/src/out02.txt" ) ;
BufferedWriter bw = new BufferedWriter ( fw) ;
bw. write ( 98 ) ;
bw. write ( 'a' ) ;
bw. write ( '徐' ) ;
bw. newLine ( ) ;
bw. write ( "abc我是中国人" ) ;
bw. newLine ( ) ;
char [ ] chars = "abc我是中国人" . toCharArray ( ) ;
bw. write ( chars) ;
bw. newLine ( ) ;
bw. write ( "abc我是中国人" , 0 , 5 ) ;
bw. newLine ( ) ;
bw. write ( chars, 3 , 5 ) ;
bw. newLine ( ) ;
bw. close ( ) ;
}
}
案例:拷贝出师表到另一个文件,恢复顺序
①定义一个缓存字符输入流管道与源文件接通。 ②定义一个List集合存储读取的每行数据。 ③定义一个循环按照行读取数据,存入到List集合中去。 ④对List集合中的每行数据按照首字符编号升序排序。 ⑤定义一个缓存字符输出管道与目标文件接通。 ⑥遍历List集合中的每个元素,用缓冲输出管道写出并换行。
public class BufferedCharTest3 {
public static void main ( String [ ] args) {
try (
BufferedReader br = new BufferedReader ( new FileReader ( "io-app2/src/csb.txt" ) ) ;
BufferedWriter bw = new BufferedWriter ( new FileWriter ( "io-app2/src/new.txt" ) ) ;
) {
List < String > data = new ArrayList < > ( ) ;
String line;
while ( ( line = br. readLine ( ) ) != null ) {
data. add ( line) ;
}
System . out. println ( data) ;
List < String > sizes = new ArrayList < > ( ) ;
Collections . addAll ( sizes, "一" , "二" , "三" , "四" , "五" , "陆" , "柒" , "八" , "九" , "十" , "十一" ) ;
Collections . sort ( data, new Comparator < String > ( ) {
@Override
public int compare ( String o1, String o2) {
return sizes. indexOf ( o1. substring ( 0 , o1. indexOf ( "." ) ) )
- sizes. indexOf ( o2. substring ( 0 , o2. indexOf ( "." ) ) ) ;
}
} ) ;
System . out. println ( data) ;
for ( String datum : data) {
bw. write ( datum) ;
bw. newLine ( ) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}
转换流
字符输入转换流:InputStreamReader
构造器 说明 public InputStreamReader(InputStream is) 可以把原始的字节流按照代码默认编码转换成字符输入流。几乎不用,与默认的FileReader一样。 public InputStreamReader(InputStream is ,String charset) 可以把原始的字节流按照指定编码转换成字符输入流,这样字符流中的字符就不乱码了(重点)
public class InputStreamReaderDemo01 {
public static void main ( String [ ] args) throws Exception {
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) ;
}
}
}
字符输出转换流:OutputStreamWriter
构造器 说明 public OutputStreamWriter(OutputStream os) 可以把原始的字节输出流按照代码默认编码转换成字符输出流。几乎不用。 public OutputStreamWriter(OutputStream os,String charset) 可以把原始的字节输出流按照指定编码转换成字符输出流(重点)
public class OutputStreamWriterDemo02 {
public static void main ( String [ ] args) throws Exception {
OutputStream os = new FileOutputStream ( "io-app2/src/out03.txt" ) ;
Writer osw = new OutputStreamWriter ( os , "GBK" ) ;
BufferedWriter bw = new BufferedWriter ( osw) ;
bw. write ( "我爱中国1~~" ) ;
bw. write ( "我爱中国2~~" ) ;
bw. write ( "我爱中国3~~" ) ;
bw. close ( ) ;
}
}
对象序列化:把对象数据存在文件中去 使用的流:对象字节输出流ObjectOutputStream
构造器 说明 public ObjectOutputStream(OutputStream out) 把低级字节输出流包装成高级的对象字节输出流
方法名称 说明 public final void writeObject(Object obj) 把对象写出去到对象序列化流的文件中去
对象必须实现序列化接口(对象中有些数据不想参与序列化如密码等,因为保存的文件可能被别人得到,会产生一些安全问题,因此,在不想序列化的成员变量前加transient
) 版本号的作用:有时候会对对象增加一些内容,然后更新版本号,这样就必须重新序列化和反序列化才会生效,也就是要强制更新数据
public class Student implements Serializable {
private static final long serialVersionUID = 1 ;
private String name;
private String loginName;
private transient String passWord;
private int age ;
}
public class ObjectOutputStreamDemo1 {
public static void main ( String [ ] args) throws Exception {
Student s = new Student ( "陈磊" , "chenlei" , "1314520" , 21 ) ;
ObjectOutputStream oos = new ObjectOutputStream ( new FileOutputStream ( "io-app2/src/obj.txt" ) ) ;
oos. writeObject ( s) ;
oos. close ( ) ;
System . out. println ( "序列化完成了~~" ) ;
}
}
对象反序列化:把文件中的对象数据重新写入内存的Java对象中 使用的流:文件字节输入流ObjectInputStream
构造器 说明 public ObjectInputStream(InputStream out) 把低级字节输如流包装成高级的对象字节输入流
方法名称 说明 public Object readObject() 把存储到磁盘文件中去的对象数据恢复成内存中的对象返回
public class ObjectInputStreamDemo2 {
public static void main ( String [ ] args) throws Exception {
ObjectInputStream is = new ObjectInputStream ( new FileInputStream ( "io-app2/src/obj.txt" ) ) ;
Student s = ( Student ) is. readObject ( ) ;
System . out. println ( s) ;
}
}
打印流
更高效的打印数据到文件中去 类型:PrintStream,PrintWriter
PrintStream
构造器 说明 public PrintStream(OutputStream os) 打印流直接通向字节输出流管道 public PrintStream(File f) 打印流直接通向文件对象 public PrintStream(String filepath) 打印流直接通向文件路径
方法 说明 public void print(Xxx xx) 打印任意类型的数据出去
构造器 说明 public PrintWriter(OutputStream os) 打印流直接通向字节输出流管道 public PrintWriter (Writer w) 打印流直接通向字符输出流管道 public PrintWriter (File f) 打印流直接通向文件对象 public PrintWriter (String filepath) 打印流直接通向文件路径
方法 说明 public void print(Xxx xx) 打印任意类型的数据出去
public class PrintDemo1 {
public static void main ( String [ ] args) throws Exception {
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 ( ) ;
}
}
public class PrintDemo2 {
public static void main ( String [ ] args) throws Exception {
System . out. println ( "锦瑟无端五十弦" ) ;
System . out. println ( "一弦一柱思华年" ) ;
PrintStream ps = new PrintStream ( "io-app2/src/log.txt" ) ;
System . setOut ( ps) ;
System . out. println ( "庄生晓梦迷蝴蝶" ) ;
System . out. println ( "望帝春心托杜鹃" ) ;
}
}
Properties属性集对象
是Map的集合,代表的是一个属性文件,可以把自己对象中的键值对信息存入到一个属性文件中去。 属性文件:后缀是.properties
结尾的文件,里面的内容都是 key=value
,后续做系统配置信息的。
构造器 说明 void load(InputStream inStream) 从输入字节流读取属性列表(键和元素对) void load(Reader reader) 从输入字符流读取属性列表(键和元素对) void store(OutputStream out, String comments) 将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法的格式写入输出字节流 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())
public class PropertiesDemo01 {
public static void main ( String [ ] args) throws Exception {
Properties properties = new Properties ( ) ;
properties. setProperty ( "admin" , "123456" ) ;
properties. setProperty ( "dlei" , "003197" ) ;
properties. setProperty ( "heima" , "itcast" ) ;
System . out. println ( properties) ;
properties. store ( new FileWriter ( "io-app2/src/users.properties" )
, "this is users!! i am very happy! give me 100!" ) ;
}
public class PropertiesDemo02 {
public static void main ( String [ ] args) throws Exception {
Properties properties = new Properties ( ) ;
System . out. println ( properties) ;
properties. load ( new FileReader ( "io-app2/src/users.properties" ) ) ;
System . out. println ( properties) ;
String rs = properties. getProperty ( "dlei" ) ;
System . out. println ( rs) ;
String rs1 = properties. getProperty ( "admin" ) ;
System . out. println ( rs1) ;
}
}
commons-io
commons-io
是apache
开源基金组织提供的一组有关IO操作的类库,可以提高IO功能开发的效率。
commons-io
工具包提供了很多有关io操作的类。有两个主要的类FileUtils, IOUtils
FileUtils
主要有如下方法:
方法名 说明 String readFileToString(File file, String encoding) 读取文件中的数据, 返回字符串 void copyFile(File srcFile, File destFile) 复制文件。 void copyDirectoryToDirectory(File srcDir, File destDir) 复制文件夹。
使用步骤:
①在项目中创建一个文件夹:lib
②将commons-io-2.6.jar
文件复制到lib
文件夹 ③在jar
文件上点右键,选择 Add as Library ->
点击OK
④在类中导包使用
public class CommonsIODemo01 {
public static void main ( String [ ] args) throws Exception {
FileUtils . deleteDirectory ( new File ( "D:\\new" ) ) ;
}
}