当前位置:网站首页>BufferedReader和BufferedWriter
BufferedReader和BufferedWriter
2022-08-05 12:59:00 【Pr Young】
其实就是优化的输入输出:
1.BufferedReader:从缓冲区中读取内容
BufferedReader temp=new BufferedReader(new InputStreamReader(System.in));
//这里输入的时候,碰到回车键就表示输入终止了,相当于只能输入一行
System.out.println("请输入内容:");
String str=temp.readLine();//表示读出一行
System.out.println(str);
new BufferReader将字符流放到字符流缓冲区之中
但是System.in得到的是字节流
所以需要通过InputStreamReader()方法将字节流转化为字符流
如果想要读入多行数据:
BufferedReader temp=new BufferedReader(new FileReader(new File("C://Users//15776//Desktop//test//人名.txt")));
//readline函数每次读入一行数据
String str1=temp.readLine();
String str2= temp.readLine();
String str3= temp.readLine();
String str4= temp.readLine();
String str5= temp.readLine();
String str6= temp.readLine();
System.out.println(str1);//mother
System.out.println(str2);//jack
System.out.println(str3);//monkey
System.out.println(str4);//孟克
System.out.println(str5);//ribuluo
System.out.println(str6);//#¥*
一个实战中遇到的例子:
没用BufferedReader,需要一个一个
//读入一个数组,本来是:
Scanner sc=new Scanner(System.in);
//输入数组的长度
int n=sc.nextInt();
int[] nums=new int[n];
for(int i=0;i<n;i++)
{
nums[i]=sc.nextInt();
}
用了BufferedReader:可以一次性
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] strs=br.readLine().split(" ");
int[] nums=new int[100];
for(int i=0;i<strs.length;i++)
{
nums[i]=Integer.parseInt(strs[i]);
}
br.close();
2.BufferedWriter:输出数据
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
bw.write(num);
bw.flush();
//这样比直接System.out.println(num)要更快很多
边栏推荐
猜你喜欢
RT-Thread记录(二、RT-Thread内核启动流程 — 启动文件和源码分析)
Sentinel introduction and use
【搜索框】通用测试用例
英创力电子IPO被终止:年营收10亿 深创投与红土是股东
AVL树大总结
RK3588+FPGA高速图像处理通信处理机解决方案
Detailed analysis of the three cluster strategies of Redis
Amazon Detective 支持 Amazon EKS 上的 Kubernetes 工作负载以进行安全调查
浅解排列与组合
115. In-depth explanation of the technical implementation of configuring the local SAP UI5 application to the local Fiori Launchpad
随机推荐
地平线初体验.上
怎么在微信上做小程序卖货?怎样做小程序卖货
AVL树大总结
C进阶-数据的存储(下)
VINS-Mono结果展示
likeshop搭建商城系统,一步到位
solaris-oralce rac 安装
C进阶 - 指针进阶
ApiPost使用教程
C进阶-动态内存管理
Easy to understand and play QT: QT program release packaging
《MySQL核心知识》第2章:MySQL常用的命令
序章 调度系统架构设计总述
怎么在微信上做小程序?不懂代码怎么开发微信小程序?
RK3588+FPGA high-speed image processing communication processor solution
Intronic Electronics IPO was terminated: annual revenue of 1 billion Shenzhen Venture Capital and Hongtu are shareholders
C进阶-自定义类型:结构体,枚举,联合
TypeScript的崛起之路给我们带来的选谷思路
LeetCode 623 在二叉树中增加一行[BFS DFS] HERODING的LeetCode之路
模拟实现(strncpy)(strncat)(atoi)(C语言)(VS)