缓冲流的概述
- 缓冲流也称为高效流、或者高级流,之前学习的字节流可以称为原始流
- 作用:缓冲流自带缓冲区、可以提高原始字节流、字符流读写数据的性能
字节缓冲输入输出流的代码实现
try (//这里面只能放置资源对象,用完会自动关闭,自动调用资源对象的close方法关闭资源(即使出现异常也会做关闭操作
//1、创建一个字节输入流与原文件接通
InputStream inputStream = new BufferedInputStream (new FileInputStream ("D:\\Users\\cczyan\\Desktop\\移通所有作业\\42页.mp4"));
//2、创建一个字节输出流与目标文件接通
OutputStream outputStream = new BufferedOutputStream (new FileOutputStream ("D:\\Users\\cczyan\\Desktop\\移通所有作业\\我是复制的视频.mp4"));) {
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read (bytes)) != -1) outputStream.write (bytes, 0, len);
} catch (Exception e) {
e.printStackTrace ();
}
字节缓冲输入输出流性能提高的体现
private static final String SRC_FILE = "D:\\Users\\cczyan\\Desktop\\移通所有作业\\29页例2.1.mp4";
private static final String DEST_FILE = "D:\\Users\\cczyan\\Desktop\\移通所有作业\\";
public static void main(String[] args) {
//使用低级的字节流按照一个一个字节的形式复制文件
oneInput ();
//使用低级的字节流按照一个一个字节数组的形式复制文件
groupInput ();
//使用字节缓冲流按照一个一个的字节的形式复制文件
oneBuffer ();
//使用字节缓冲流按照一个一个的字节数组的形式复制文件
groupBuffer ();
}
private static void groupBuffer() {
try (
InputStream inputStream = new BufferedInputStream (new FileInputStream (SRC_FILE));
OutputStream outputStream = new BufferedOutputStream (new FileOutputStream (DEST_FILE + "字节缓冲流的数组字节复制.mp4"));
) {
Long startTime = System.currentTimeMillis ();
byte[] e = new byte[1024];
int ch;
while ((ch = inputStream.read (e)) != -1) {
outputStream.write (e, 0, ch);
}
Long endTime = System.currentTimeMillis ();
System.out.println ("字节缓冲流的数组字节复制:" + (endTime - startTime) / 1000.0 + "s");
} catch (Exception e) {
e.printStackTrace ();
}
}
private static void oneBuffer() {
try (
InputStream inputStream = new BufferedInputStream (new FileInputStream (SRC_FILE));
OutputStream outputStream = new BufferedOutputStream (new FileOutputStream (DEST_FILE + "字节缓冲流的单字节复制.mp4"));
) {
Long startTime = System.currentTimeMillis ();
int ch;
while ((ch = inputStream.read ()) != -1) {
outputStream.write (ch);
}
Long endTime = System.currentTimeMillis ();
System.out.println ("字节缓冲流的单字节复制:" + (endTime - startTime) / 1000.0 + "s");
} catch (Exception e) {
e.printStackTrace ();
}
}
private static void groupInput() {
try (
InputStream inputStream = new FileInputStream (SRC_FILE);
OutputStream outputStream = new FileOutputStream (DEST_FILE + "低级字节流的数组字节复制.mp4");
) {
Long startTime = System.currentTimeMillis ();
byte[] e = new byte[1024];
int ch;
while ((ch = inputStream.read (e)) != -1) {
outputStream.write (e, 0, ch);
}
Long endTime = System.currentTimeMillis ();
System.out.println ("低级字节流的数组字节复制:" + (endTime - startTime) / 1000.0 + "s");
} catch (Exception e) {
e.printStackTrace ();
}
}
private static void oneInput() {
try (
InputStream inputStream = new FileInputStream (SRC_FILE);
OutputStream outputStream = new FileOutputStream (DEST_FILE + "低级字节流的单字节复制.mp4");
) {
Long startTime = System.currentTimeMillis ();
int ch;
while ((ch = inputStream.read ()) != -1) {
outputStream.write (ch);
}
Long endTime = System.currentTimeMillis ();
System.out.println ("低级字节流的单字节复制所需时间:" + (endTime - startTime) / 1000.0 + "s");
} catch (Exception e) {
e.printStackTrace ();
}
}
字符缓冲输入输出流
作用
提高字符输入输出流读取数据的性能,除此之外多了按照行读取数据的功能
构造器
- public BufferedReader(Reader r):可以把低级的字符输入流包装成一个高级的缓冲字符输入流管道,从而提高字符输入流读数据的性能
方法
- publlic String readLine():读取一行数据返回,如果读取没有完毕,无行可读返回null
try (
BufferedReader reader = new BufferedReader (new FileReader ("src\\cs1.txt"));
) {
String ch;
while ((ch = reader.readLine ()) != null) {
System.out.println (ch);
}
} catch (Exception e) {
e.printStackTrace ();
}
- void newLine():BufferWriter提供的换行方法
try (
BufferedWriter bufferedWriter = new BufferedWriter (new FileWriter ("src\\cs1.txt"));
) {
bufferedWriter.write ("我是第一行");
bufferedWriter.newLine ();//换行
bufferedWriter.write ("我是第二行");
bufferedWriter.newLine ();
bufferedWriter.write ("我是第三行");
bufferedWriter.newLine ();
} catch (Exception e) {
e.printStackTrace ();
}
代码实现
try (
Reader reader = new BufferedReader (new FileReader ("src\\cs1.txt"));
) {
// System.out.println ((char) reader.read ());
// System.out.println ((char) reader.read ());
char[] ch = new char[1024];
int len;
while ((len = reader.read (ch)) != -1) {
System.out.println (new String (ch, 0, len));
}
} catch (Exception e) {
e.printStackTrace ();
}
}
例子
将乱序文章进行排序并复制到新的文件里面
try (
BufferedReader bufferedReader = new BufferedReader (new FileReader (ESC_SS));
BufferedWriter bufferedWriter = new BufferedWriter (new FileWriter (END_SS + "我是新的.txt"));
) {
String ch;
List<String> list = new ArrayList<> ();
while ((ch = bufferedReader.readLine ()) != null) {
list.add (ch);
}
// 排序
Collections.sort (list);
System.out.println (list);
for (String an : list) {
System.out.println (an);
bufferedWriter.write (an);
bufferedWriter.newLine ();
}
} catch (Exception e) {
e.printStackTrace ();
}