java基础-IO流(缓冲流)

字节缓冲流:

复制代码
利用字节缓冲流一次读取一个字节:
public class IOTest01 {
    public static void main(String[] args) throws IOException {
        //创建字节缓冲输入流对象
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\test\\test.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\test\\test1.txt"));
        int read;
        while((read=bis.read())!=-1){
            bos.write((char)read);
        }
        bis.close();
        bos.close();
    }
}

利用字节缓冲流一次读取一个字节数组:

复制代码
public class IOTest02 {
    public static void main(String[] args) throws IOException {
        //一次读取一个字节数组
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\test\\test.txt"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\test\\test1.txt"));

        byte[] arr=new byte[1024];
        int read;
        while((read=bis.read(arr))!=-1){
            bos.write(arr,0,read);
        }
        bis.close();
        bos.close();
    }

字符缓冲流:

全部读取可以这么写

复制代码
String s;
while((s = br.readLine())!=null){
    System.out.println(s);
}
复制代码
public class IOTest04 {
    public static void main(String[] args) throws IOException {
        //
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\test\\test1.txt"));
        bw.write("微笑面对生活");
        bw.newLine();
        bw.write("恐惧会自己消失");
        bw.newLine();
        bw.close();

    }
}

练习一:

四种方式拷贝文件,并统计用时

复制代码
//1、利用字节流一次读写一个字节,拷贝文件
FileInputStream fis = new FileInputStream("E:\\test\\test.txt");
FileOutputStream fos = new FileOutputStream("E:\\test\\test1.txt");
int i;
while((i=fis.read())!=-1){
    fos.write(i);
}
fis.close();
fos.close();
复制代码
//2、利用字节数组读写文件,拷贝文件
FileInputStream fis = new FileInputStream("E:\\test\\test.txt");
FileOutputStream fos = new FileOutputStream("E:\\test\\test2.txt");
byte[] read=new byte[1024*8];
int len;
while((len=fis.read(read))!=-1){
    fos.write(read,0,len);
}
fis.close();
fos.close();
复制代码
//3、字节缓冲流一次读取一个字节,拷贝文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\test\\test.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\test\\test3.txt"));
int i;
while((i=bis.read())!=-1){
    bos.write(i);
}
bis.close();
bos.close();
复制代码
//4、字节缓冲流一次读取一个字节数组,拷贝文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\test\\test.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\test\\test4.txt"));
byte[] read = new byte[1024];
int len;
while((len=bis.read(read))!=-1){
    bos.write(read,0,len);
}
bis.close();
bos.close();

练习二:恢复出师表的顺序

复制代码
package Day10_IO;

import java.io.*;
import java.util.*;

public class IOTest02 {
    public static void main(String[] args) throws IOException {
        //恢复乱序数据《出师表》
        BufferedReader br = new BufferedReader(new FileReader("E:\\test\\test.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\test\\test1.txt"));
        String s;
        //定义一个双列集合来存储
        HashMap<Integer, String> hash = new HashMap<>();
        while((s=br.readLine())!=null){
            String head = s.substring(0,1);//获取到每一行的首数字
            hash.put(Integer.parseInt(head),s);//放到双列集合中
        }
        //把hashMap的键值对转成单列集合
        List<Map.Entry<Integer, String>> entryList = new ArrayList<>(hash.entrySet());
        Collections.sort(entryList,Map.Entry.comparingByKey());
        //
        for (Map.Entry<Integer, String> integerStringEntry : entryList) {
            bw.write(integerStringEntry.getValue());
            bw.newLine();
        }
        bw.close();
        br.close();
    }
}

上面的双列集合,也可以用treeMap就自动排序了

复制代码
package Day10_IO;

import java.io.*;

public class IOTest03 {
    public static void main(String[] args) throws IOException {
        //判断程序登录的次数
        BufferedReader br = new BufferedReader(new FileReader("E:\\test\\count.txt"));
        int count = Integer.parseInt(br.readLine());//记录现在文件中的访问次数
        //判断
        if(count<=3){
            System.out.println("欢迎使用本软件,第"+count+"次使用免费");
            //每执行一次后,需要讲count++执行到原文件
            count=count+1;
            BufferedWriter bos = new BufferedWriter(new FileWriter("E:\\test\\count.txt"));
            System.out.println(count);
            bos.write(String.valueOf(count));
            bos.close();
        }else {
            System.out.println("本软件只能免费使用3次,欢迎注册会员后继续使用");
        }
        br.close();
    }
}
相关推荐
凉辰几秒前
解决 H5 键盘遮挡与页面上推
开发语言·javascript·计算机外设
计算机安禾35 分钟前
【c++面向对象编程】第25篇:仿函数(函数对象):重载operator()
开发语言·c++·算法
Rust语言中文社区36 分钟前
【Rust日报】2026-05-14 Pyrefly v1.0 正式发布:快速的 Python 类型检查器和语言服务器
开发语言·后端·python·rust
吴声子夜歌41 分钟前
Java——定时任务
java
吴声子夜歌1 小时前
Java——原子变量和CAS
java·cas
野生技术架构师1 小时前
2026最新Java面试八股文天花板(含详细解析)
java·jvm·spring
kkeeper~1 小时前
0基础C语言积跬步之深入理解指针(4)
c语言·开发语言
小碗羊肉1 小时前
【JavaWeb | 第十二篇】项目实战——登录功能
java·前端·数据库
周末也要写八哥1 小时前
在C++中使用预定义宏
开发语言·c++·算法
喜欢小苹果的码农1 小时前
Java动态定时任务
java