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();
    }
}
相关推荐
quantdash_cc27 分钟前
Python 股票 K 线数据质量校验:字段、缺失值、重复行和价格异常
开发语言·python·数据分析·量化交易·股票数据·quantdash
Zenova EdgeOS28 分钟前
工业网关重试机制:从固定间隔到指数退避的工程实战
开发语言·网络·php
嘻哈baby1 小时前
Go 函数中的参数为什么不支持默认值?
java·开发语言·jvm
慧都小项1 小时前
MyEclipse 2026:当 Agent、MCP 与 Java 26 进入 Eclipse 工作流
java·eclipse·springboot·copilot·myeclipse
一晌小贪欢1 小时前
python-第29天:Python面向对象之多态与抽象类
开发语言·python·数据可视化·面向对象·python办公·python多态
Chester_19991 小时前
CSP202312C.树上搜索
开发语言·数据结构·c++·蓝桥杯·stl
Figo_Cheung1 小时前
Figo共振网络宇宙演化论(RNC) :从原初对称破缺到全息大和谐的演化路径研究
开发语言·php·量子计算
重生之我来学Python1 小时前
Git-SVN 混合开发,从入门到精通!
开发语言·git·svn·github
CoderYanger1 小时前
前端基础——JavaScript(基础语法)(下篇)
java·开发语言·前端·javascript·程序人生·面试·职场和发展
京师20万禁军教头2 小时前
39面向对象(高级)-设计模式
java·开发语言·设计模式