缓冲流原理
package com.mujiachao.huanchongliu;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferOut {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("F:\\电影\\111\\F.txt");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bufferedOutputStream.write("我把数据通过字节缓冲输出流写入硬盘".getBytes());
bufferedOutputStream.close();
}
}
字节缓冲输入流
package com.mujiachao.huanchongliu;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class BufferIn {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("F:\\电影\\111\\F.txt");
// BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream,2048);
// int len=0;
// while ((len=bufferedInputStream.read())!=-1){
// System.out.println(len);
// }
int len=0;
byte[] bytes=new byte[2048];
while ((len=bufferedInputStream.read(bytes))!=-1){
System.out.println(new String(bytes));
}
bufferedInputStream.close();
}
}
缓冲流效率测试
package com.mujiachao.huanchongliu;
import java.io.*;
public class CopyCeSu {
public static void main(String[] args) throws IOException {
long l = System.currentTimeMillis();
FileOutputStream fileOutputStream = new FileOutputStream("F:\\电影\\111\\JavaEE开发的颠覆者 Spring Boot实战 完整版.pdf");
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\JavaEE开发的颠覆者 Spring Boot实战 完整版.pdf");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
int len=0;
byte[] bytes=new byte[102400];
while ((len=bufferedInputStream.read(bytes))!=-1){
bufferedOutputStream.write(len);
}
bufferedOutputStream.close();
bufferedInputStream.close();
System.out.println(System.currentTimeMillis()-l+"毫秒");
}
}
字符缓冲输出流 bufferedWriter.newLine();换行
package com.mujiachao.huanchongliu;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferWriter {
public static void main(String[] args) throws IOException {
FileWriter fileWriter = new FileWriter("F:\\电影\\111\\a.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (int i = 0; i < 10; i++) {
bufferedWriter.write("字符缓冲输出流");
bufferedWriter.newLine();
}
bufferedWriter.close();
}
}
字符缓冲输入流 bufferedReader.readLine() 读取一行
package com.mujiachao.huanchongliu;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferReader {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\电影\\111\\a.txt"));
String s;
while ((s=bufferedReader.readLine())!=null){
System.out.println(s);
}
bufferedReader.close();
}
}
练习,对文本内容进行排序
package com.mujiachao.huanchongliu;
import java.io.*;
import java.util.HashMap;
public class LianXi {
public static void main(String[] args) throws IOException {
HashMap<String, String> objectObjectHashMap = new HashMap<>();
FileReader fileReader = new FileReader("F:\\电影\\111\\in.txt");
FileWriter fileWriter = new FileWriter("F:\\电影\\111\\out.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
String s;
while ((s=bufferedReader.readLine())!=null){
String[] split = s.split("\\.");
objectObjectHashMap.put(split[0],split[1]);
}
for (String key:objectObjectHashMap.keySet()) {
s=key+"."+objectObjectHashMap.get(key);
bufferedWriter.write(s);
bufferedWriter.newLine();
}
bufferedWriter.close();
bufferedReader.close();
}
}