当前位置:网站首页>Input stream in io stream
Input stream in io stream
2022-07-25 10:15:00 【Look at the bugs】
io Stream profile
i:input o:output
io Stream is used to deal with the problem of device data transmission
Input stream : Reading data
Output stream : Writing data
Byte stream : Byte input stream , Byte output stream
Character stream : Character input stream , Character output stream
If the data passes through window The self-contained Notepad can be opened , The content inside can be read , Is the character stream
No, it's byte stream ( If you don't know what to use, use byte stream )
Byte input stream ( Writing data )
Steps to write data using byte output stream :
1, Call the system to create a file
2, Created byte output stream object
3, Let the byte output stream object point to the created file
// Create byte stream output stream object
//FileOutputStream (String name), Create a file output stream and write the specified name to the file
//IDEA Default myByteStream So you don't have to enter
FileOutputStream fos =new FileOutputStream("fos.txt");
//void write(int b): Write the specified bytes to this file output stream
fos.write(97);
fos.write(57);
fos.write(55);
// In the end, we need to release resources
//void close(), Close this file output stream / And release any system resources related to this flow
fos.close();
How to write data
// The first way is to write directly
fos.write(97);
fos.write(98);
fos.write(99);
fos.write(100);
// The second way is to write data through the situation of the array
void write(byte[] b); take b.length Byte writes the output stream of this file from the specified byte array
byte[]bys={
97,98,87,99,100};
fos.write(bys);
// Write by string
byte[]getBytes(), Returns the byte array corresponding to the string
byte[] bys="abcde".getBytes();
fos.write(bys);
// Write data at the specified position and length
//void write (byte[] b,int off,int len);byte[] b Is the name of the specified array ,
//int off Index subscript ,int len Refers to the length of the written data ;
// example (1,3) Is to write the index in the array as 1 The next three numbers are 1 The index for ,2,3
byte[]bys={
97,98,87,99,100};
fos.write(bys,1,3);
// Standard way of writing data
for (int i = 0; i < 10; i++) {
fos.write("hello".getBytes());
fos.write("\n".getBytes());
}
fos.close();
Append write data
// From a to Z , Rewrite all data , Overlay the previous data
FileOutputStream fos =new FileOutputStream(“fos.txt”);
// Add data from the end
FileOutputStream fos =new FileOutputStream(“fos.txt”,true);
Exception handling and judge to close resources
try {
FileOutputStream fos = new FileOutputStream("fos.txt", true);
// Write data
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();
}
边栏推荐
猜你喜欢
随机推荐
Probabilistic robot learning notes Chapter 2
RedisUtil
Usage of string slicing
UE4 碰撞(Collsion)
Dynamic planning, shopping list problem
静态路由的配置(以华为eNSP为例)
IO流中的输出流
mysql历史数据补充新数据
ROS distributed operation -- launch file starts nodes on multiple machines
Use and principle of rest
struct2的原理
Pytorch 张量列表转换为张量 List of Tensor to Tensor 使用 torch.stack()
数论--负进制转换
Ubuntu20.04系统下安装MySQL数据库5.7.29版本
GCD详解
VoxCeleb1 数据集下载
多线程——静态代理模式
多线程——五大状态
[machine translation] scones -- machine translation with multi tag tasks
nodejs版本升级或切换的常用方式









