当前位置:网站首页>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();
}
边栏推荐
猜你喜欢
随机推荐
CCF 201512-3 drawing
代码整洁之道--直击痛点
Detailed explanation of JDBC operation database
SSM整合(简单的图书管理系统来整合SSM)
[recommended collection] with these learning methods, I joined the world's top 500 - the "fantastic skills and extravagance" in the Internet age
Eco introduction
js利用requestAnimationFrame实时检测当前动画的FPS帧率
dp-851
1、 Initial mysql, MySQL installation, environment configuration, initialization
Filter filter details (listeners and their applications)
Arm preliminaries
LOAM 融合 IMU 细节之 TransformToEnd 函数
Debug篇快捷键入门
小程序分享功能
Probability theory and mathematical statistics 4 continuous random variables and probability distributions (Part 1)
ThreadLocal&Fork/Join
Introduction to Verdi Foundation
VLAN的配置及其应用(以华为eNSP为例)
message from server: “Host ‘xxx.xxx.xxx.xxx‘ is not allowed to connect to this MySQL server“
Configuring ROS development environment with vscode: Causes and solutions to the problem of ineffective code modification







![[machine translation] scones -- machine translation with multi tag tasks](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)

