当前位置:网站首页>IO流中的输入流
IO流中的输入流
2022-07-25 09:27:00 【看小虫子】
io流简介
i:input o:output
io流是用来处理设备数据传输问题的
输入流:读数据
输出流:写数据
字节流:字节输入流,字节输出流
字符流:字符输入流,字符输出流
如果数据通过window自带的记事本可以打开,里面内容能够读懂,就是字符流
不是就是字节流(不知道用那个就用字节流)
字节输入流(写数据)
使用字节输出流写数据的步骤:
1,调用系统创建了文件
2,创建了字节输出流对象
3,让字节输出流对象指向创建好的文件
//创建字节流输出流对象
//FileOutputStream (String name),创建文件输出流指定的名称写入文件
//IDEA默认myByteStream所以不用输入
FileOutputStream fos =new FileOutputStream("fos.txt");
//void write(int b): 将制定的字节写入此文件输出流
fos.write(97);
fos.write(57);
fos.write(55);
//最后都要释放资源
//void close(),关闭此文件输出流/并释放与此流相关的任何系统资源
fos.close();
写入数据的方式
//第一种方式直接写入
fos.write(97);
fos.write(98);
fos.write(99);
fos.write(100);
//第二种方式通过数组的形势写入数据
void write(byte[] b);将b.length字节从指定的字节数组写入此文件输出流
byte[]bys={
97,98,87,99,100};
fos.write(bys);
//通过字符串的方式写入
byte[]getBytes(),返回字符串对应的字节数组
byte[] bys="abcde".getBytes();
fos.write(bys);
//写入指定位置和长度的数据
//void write (byte[] b,int off,int len);byte[] b是指定数组的名字,
//int off指代索引下标,int len指代写入数据的长度;
// 例(1,3)就是写入数组中索引为1往后的三个数即1索引为,2,3
byte[]bys={
97,98,87,99,100};
fos.write(bys,1,3);
//标准写入数据的方式
for (int i = 0; i < 10; i++) {
fos.write("hello".getBytes());
fos.write("\n".getBytes());
}
fos.close();
追加写入数据
//从头到尾,重新写入所有数据,覆盖之前的数据
FileOutputStream fos =new FileOutputStream(“fos.txt”);
//从最后添加数据
FileOutputStream fos =new FileOutputStream(“fos.txt”,true);
异常处理和判断关闭资源
try {
FileOutputStream fos = new FileOutputStream("fos.txt", true);
//写入数据
for (int i = 0; i < 10; i++) {
fos.write("hello".getBytes());
fos.write("\n".getBytes());
}
if(fos!=null){
fos.close();
}
}catch (IOException e){
e.printStackTrace();
}
边栏推荐
猜你喜欢

Advanced introduction to digital IC Design SOC
![[recommended collection] with these learning methods, I joined the world's top 500 - the](/img/95/e34473a1628521d4b07e56877fcff1.png)
[recommended collection] with these learning methods, I joined the world's top 500 - the "fantastic skills and extravagance" in the Internet age

线程池的死锁事件

车辆属性最近一次入库时间初始化生成sql脚本文件

小程序H5获取手机号方案

JDBC总结

UE4 LoadingScreen动态加载启动动画

Introduction to low power consumption and UPF

概率论与数理统计 4 Continuous Random Variables and Probability Distributions(连续随机变量与概率分布)(上篇)

无线中继采集仪的常见问题
随机推荐
ThreadLocal&Fork/Join
LOAM 融合 IMU 细节之 TransformToEnd 函数
I2C也可总线取电!
Subtotal of rospy odometry sinkhole
小程序H5获取手机号方案
@Import, conditional and @importresource annotations
TM1638 LED数码显示模块ARDUINO驱动代码
C3D模型pytorch源码逐句详析(三)
T5 paper summary
ROS分布式操作--launch文件启动多个机器上的节点
MVC three-tier architecture understanding
Es6详解
Excel导入导出源码分析
Download and installation of QT 6.2
Is binary cross entropy really suitable for multi label classification?
四舍五入取近似值
Pnpm Brief
Introduction to armv8 general timer
emmet语法速查 syntax基本语法部分
Introduction to testbench