Java File和IO流
5. 缓冲流
5.1 概述
5.2 字节缓冲流
// 初始data文件内容为:a爱b
public class Test {
public static void main(String[] args) {
try (
// 这里面只能放置资源对象,用完会自动关闭:自动调用资源对象的close方法关闭资源(即使出现异常也会做关闭操作)
// 1、创建一个字节输入流管道与原视频接通
InputStream is = new FileInputStream("src/com/data");
// a.把原始的字节输入流包装成高级的缓冲字节输入流
InputStream bis = new BufferedInputStream(is);
// 2、创建一个字节输出流管道与目标文件接通
OutputStream os = new FileOutputStream("src/com/newdata");
// b.把字节输出流管道包装成高级的缓冲字节输出流管道
OutputStream bos = new BufferedOutputStream(os);
) {
// 3、定义一个字节数组转移数据
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();
}
}
}
//现在newdata文件的内容如下:
// a爱b
public class Test {
private static final String SRC_FILE = "D:\\学习视频堆放区\\20-5:模型选择和效果比较.vep";
private static final String DEST_FILE = "D:\\";
public static void main(String[] args) {
// copy01(); // 使用低级的字节流按照一个一个字节的形式复制文件:慢的让人简直无法忍受。直接被淘汰。
// copy02(); // 使用低级的字节流按照一个一个字节数组的形式复制文件: 比较慢,但是还是可以忍受的!
// copy03(); // 缓冲流一个一个字节复制:很慢,不建议使用。
copy04(); // 缓冲流一个一个字节数组复制:飞快,简直太完美了(推荐使用)
}
private static void copy04() {
long startTime = System.currentTimeMillis();
try (
// 1、创建低级的字节输入流与源文件接通
InputStream is = new FileInputStream(SRC_FILE);
// a.把原始的字节输入流包装成高级的缓冲字节输入流
InputStream bis = new BufferedInputStream(is);
// 2、创建低级的字节输出流与目标文件接通
OutputStream os = new FileOutputStream(DEST_FILE + "video4.avi");
// b.把字节输出流管道包装成高级的缓冲字节输出流管道
OutputStream bos = new BufferedOutputStream(os);
) {
// 3、定义一个字节数组转移数据
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 (
// 1、创建低级的字节输入流与源文件接通
InputStream is = new FileInputStream(SRC_FILE);
// a.把原始的字节输入流包装成高级的缓冲字节输入流
InputStream bis = new BufferedInputStream(is);
// 2、创建低级的字节输出流与目标文件接通
OutputStream os = new FileOutputStream(DEST_FILE + "video3.avi");
// b.把字节输出流管道包装成高级的缓冲字节输出流管道
OutputStream bos = new BufferedOutputStream(os);
){
// 3、定义一个变量记录每次读取的字节(一个一个字节的复制)
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 (
// 这里面只能放置资源对象,用完会自动关闭:自动调用资源对象的close方法关闭资源(即使出现异常也会做关闭操作)
// 1、创建一个字节输入流管道与原视频接通
InputStream is = new FileInputStream(SRC_FILE);
// 2、创建一个字节输出流管道与目标文件接通
OutputStream os = new FileOutputStream(DEST_FILE + "video2.avi")
) {
// 3、定义一个字节数组转移数据
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 (
// 1、创建低级的字节输入流与源文件接通
InputStream is = new FileInputStream(SRC_FILE);
// 2、创建低级的字节输出流与目标文件接通
OutputStream os = new FileOutputStream(DEST_FILE + "video1.avi")
){
// 3、定义一个变量记录每次读取的字节(一个一个字节的复制)
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");
}
}
5.3 字符缓冲流
// 初始data文件内容为:a爱b
public class Test {
public static void main(String[] args) {
try (
// 1、创建一个文件字符输入流与源文件接通。
Reader fr = new FileReader("src/com/data");
// a、把低级的字符输入流包装成高级的缓冲字符输入流。
BufferedReader br = new BufferedReader(fr);
){
// 2、用循环,每次读取一个字符数组的数据。
/**
char[] buffer = new char[1024]; // 1K字符
int len;
while ((len = br.read(buffer)) != -1) {
String rs = new String(buffer, 0, len);
System.out.print(rs);
}
*/
String line;
while ((line = br.readLine()) != null){
System.out.println(line); //a爱b
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 初始data文件内容为:a爱b
public class Test {
public static void main(String[] args) throws Exception {
// 1、创建一个字符输出流管道与目标文件接通
Writer fw = new FileWriter("src/com/data"); // 覆盖管道,每次启动都会清空文件之前的数据
//Writer fw = new FileWriter("src/com/data", true); // 追加数据
BufferedWriter bw = new BufferedWriter(fw);
// a.public void write(int c):写一个字符出去
bw.write(98);
bw.write('a');
bw.write('徐'); // 不会出问题了
bw.newLine(); // bw.write("\r\n"); // 换行
// b.public void write(String c)写一个字符串出去
bw.write("abc我是中国人");
bw.newLine(); // bw.write("\r\n"); // 换行
// c.public void write(char[] buffer):写一个字符数组出去
char[] chars = "abc我是中国人".toCharArray();
bw.write(chars);
bw.newLine(); // bw.write("\r\n"); // 换行
// d.public void write(String c ,int pos ,int len):写字符串的一部分出去
bw.write("abc我是中国人", 0, 5);
bw.newLine(); // bw.write("\r\n"); // 换行
// e.public void write(char[] buffer ,int pos ,int len):写字符数组的一部分出去
bw.write(chars, 3, 5);
bw.newLine(); // bw.write("\r\n"); // 换行
// fw.flush();// 刷新后流可以继续使用
bw.close(); // 关闭包含刷线,关闭后流不能使用
}
}
public class Test {
public static void main(String[] args) {
try(
// 1、创建缓冲字符输入流管道与源文件接通
BufferedReader br = new BufferedReader(new FileReader("D:\\博学谷java\\day20、IO流二\\代码\\io-app2\\src\\csb.txt"));
// 5、定义缓冲字符输出管道与目标文件接通
BufferedWriter bw = new BufferedWriter(new FileWriter("src/com/data"));
) {
// 2、定义一个List集合存储每行内容
List<String> data = new ArrayList<>();
// 3、定义循环,按照行读取文章
String line;
while ((line = br.readLine()) != null){
data.add(line);
}
System.out.println(data);
// 4、排序
// 自定义排序规则
List<String> sizes = new ArrayList<>();
Collections.addAll(sizes, "一","二","三","四","五","陆","柒","八","九","十","十一");
Collections.sort(data, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// o1 八.,....
// o2 柒.,....
return sizes.indexOf(o1.substring(0, o1.indexOf(".")))
- sizes.indexOf(o2.substring(0, o2.indexOf(".")));
}
});
System.out.println(data);
// 6、遍历集合中的每行文章写出去,且要换行
for (String datum : data) {
bw.write(datum);
bw.newLine(); // 换行
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
6. 转换流
6.1 问题引出 - 不同编码读取乱码问题
6.2 字符输入转换流
public class Test {
public static void main(String[] args) throws Exception {
// 代码:UTF-8 文件:GBK "D:\\resources\\data.txt"
// 1、提取GBK文件的原始字节流。
InputStream is = new FileInputStream("D:\\resources\\data.txt");
// 2、把原始字节流转换成字符输入流
// Reader isr = new InputStreamReader(is); // 默认以UTF-8的方式转换成字符流。 还是会乱码的 跟直接使用FileReader是一样的
Reader isr = new InputStreamReader(is , "GBK"); // 以指定的GBK编码转换成字符输入流 完美的解决了乱码问题
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null){
System.out.println(line);
}
}
}
6.3 字符输出转换流
public class Test {
public static void main(String[] args) throws Exception {
// 1、定义一个字节输出流
OutputStream os = new FileOutputStream("src/com/data");
// 2、把原始的字节输出流转换成字符输出流
// Writer osw = new OutputStreamWriter(os); // 以默认的UTF-8写字符出去 跟直接写FileWriter一样
Writer osw = new OutputStreamWriter(os , "GBK"); // 指定GBK的方式写字符出去
// 3、把低级的字符输出流包装成高级的缓冲字符输出流。
BufferedWriter bw = new BufferedWriter(osw);
bw.write("我爱中国1~~");
bw.write("我爱中国2~~");
bw.write("我爱中国3~~");
bw.close();
}
}
7. 序列化对象
7.1 对象序列化
public class Test {
public static void main(String[] args) throws Exception {
// 1、创建学生对象
Student s = new Student("陈磊", "chenlei","1314520", 21);
// 2、对象序列化:使用对象字节输出流包装字节输出流管道
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src/com/data"));
// 3、直接调用序列化方法
oos.writeObject(s);
// 4、释放资源
oos.close();
System.out.println("序列化完成了~~");
}
}
/**
对象如果要序列化,必须实现Serializable序列化接口。
*/
public class Student implements Serializable {
// 申明序列化的版本号码
// 序列化的版本号与反序列化的版本号必须一致才不会出错!
private static final long serialVersionUID = 1;
private String name;
private String loginName;
// transient修饰的成员变量不参与序列化了
private transient String passWord;
private int age ;
public Student(){
}
public Student(String name, String loginName, String passWord, int age) {
this.name = name;
this.loginName = loginName;
this.passWord = passWord;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", loginName='" + loginName + '\'' +
", passWord='" + passWord + '\'' +
", age=" + age +
'}';
}
}
7.2 对象反序列化
public class Test {
public static void main(String[] args) throws Exception {
// 1、创建对象字节输入流管道包装低级的字节输入流管道
ObjectInputStream is = new ObjectInputStream(new FileInputStream("src/com/data"));
// 2、调用对象字节输入流的反序列化方法
Student s = (Student) is.readObject();
System.out.println(s);
}
}
8. 打印流
8.1 PrintStream、PrintWriter
public class Test {
public static void main(String[] args) throws Exception {
// 1、创建一个打印流对象
// PrintStream ps = new PrintStream(new FileOutputStream("src/com/data"));
// PrintStream ps = new PrintStream(new FileOutputStream("src/com/data" , true)); // 追加数据,在低级管道后面加True
// 继续优化:直接省略低级管道
// PrintStream ps = new PrintStream("src/com/data" );
PrintWriter ps = new PrintWriter("src/com/data"); // 打印功能上与PrintStream的使用没有区别
ps.println(97);
ps.println('a');
ps.println(23.3);
ps.println(true);
ps.println("我是打印流输出的,我是啥就打印啥");
ps.close();
}
}
8.2 输出语句的重定向
// 初始data文件内容为空
public class Test {
public static void main(String[] args) throws Exception {
System.out.println("锦瑟无端五十弦");
System.out.println("一弦一柱思华年");
// 改变输出语句的位置(重定向)
PrintStream ps = new PrintStream("src/com/data");
System.setOut(ps); // 把系统打印流改成我们自己的打印流
System.out.println("庄生晓梦迷蝴蝶");
System.out.println("望帝春心托杜鹃");
}
}
//控制台内容:
//锦瑟无端五十弦
//一弦一柱思华年
//现在newdata文件的内容如下:
//庄生晓梦迷蝴蝶
//望帝春心托杜鹃
9. Properties
// 初始data文件内容为空
public class Test {
public static void main(String[] args) throws Exception {
// 需求:使用Properties把键值对信息存入到属性文件中去。
Properties properties = new Properties();
properties.setProperty("admin", "123456");
properties.setProperty("dlei", "003197");
properties.setProperty("heima", "itcast");
System.out.println(properties);
// {dlei=003197, admin=123456, heima=itcast}
/**
参数一:保存管道 字符输出流管道
参数二:保存心得
*/
properties.store(new FileWriter("src/com/data")
, "happy to learn Java!");
}
}
//现在newdata文件的内容如下:
// #happy to learn Java!
// #Thu Dec 16 10:55:07 CST 2021
// dlei=003197
// admin=123456
// heima=itcast
// 初始data文件内容为:
// #happy to learn Java!
// #Thu Dec 16 10:55:07 CST 2021
// dlei=003197
// admin=123456
// heima=itcast
public class Test {
public static void main(String[] args) throws Exception {
// 需求:Properties读取属性文件中的键值对信息。(读取)
Properties properties = new Properties();
System.out.println(properties); // {}
// 加载属性文件中的键值对数据到属性对象properties中去
properties.load(new FileReader("src/com/data"));
System.out.println(properties); // {dlei=003197, admin=123456, heima=itcast}
String rs = properties.getProperty("dlei");
System.out.println(rs); //003197
String rs1 = properties.getProperty("admin");
System.out.println(rs1); //123456
}
}
10. IO框架
public class Test {
public static void main(String[] args) throws Exception {
// 1.完成文件复制!
IOUtils.copy(new FileInputStream("src/com/data"),
new FileOutputStream("src/com/newdata"));
// 2.完成文件复制到某个文件夹下!
FileUtils.copyFileToDirectory(new File("src/com/data"), new File("D:/"));
// 3.完成文件夹复制到某个文件夹下!
FileUtils.copyDirectoryToDirectory(new File("D:\\resources") , new File("D:\\new"));
FileUtils.deleteDirectory(new File("D:\\new"));
// JDK1.7 自己也做了一些一行代码完成复制的操作:New IO的技术
// Files.copy(Path.of("D:\\resources\\hushui.jpeg"), Path.of("D:\\resources\\hushui3.jpeg"));
// 4. 删除文件夹
FileUtils.deleteDirectory(new File("D:\\new"));
}
}