当前位置:网站首页>27io stream, byte output stream, OutputStream writes data to file
27io stream, byte output stream, OutputStream writes data to file
2022-07-06 05:26:00 【dengfengling999】
Hard disk permanent storage , The data stored in the hard disk is bytes 、 Memory : Temporary storage
IO summary
I:input( Input ) Read
O:output( Output ) write in
flow : data ( character 、 byte ) 1 Characters =2 Bytes =8 Binary bits
Input : It is to read the data in the hard disk into the memory for use
Output : Write the data in memory to the hard disk to save
According to the data, it is divided into : Character stream 、 Byte stream
Byte stream : Byte input stream :InputStream Byte output stream :OutputStream
Character stream : Character input stream :Read Character output stream :Writer
Byte stream :
Everything is bytes :
All file data ( Text 、 Images 、 video ) In storage , Are stored in binary form , It's all a byte , So it's the same when it's transmitted , So byte stream can transfer any file data , When operating the flow , We need to be clear at all times , No matter what flow object is used , The underlying transmission is always binary data
Byte output stream OutputStream
Java.io.OutputStream This abstract class is a superclass that represents all classes of all output streams ( Parent class ) Write the specified byte information to the destination
Method :
close() Close this output stream and release all system resources related to this stream
flush() Refresh the output stream once and force all buffered output bytes to be written out
write() Yes 3 individual Write data to hard disk
write(byte[] b) take b.length Bytes from specified Of byte Array Write this output stream
public void' write(byte[] b): take b.length Byte writes this output stream from the specified byte array If the first byte written is a positive number (0-127), Then the display will query ASCII surface If the first byte written is negative , The first byte and the second byte will form a Chinese display , Query the system default code table (GBK)
write(byte[] b,int off,int len ) Writes the specified byte array to len Bytes , From the offset off Start the output stream to this
public void write(byte [] b,int off,int len): Write part of the byte array to a file
int off: Start index of array
int len: Write a few bytes
The specified byte array is written len byte , From the offset off Start output to this output stream
write(int b) Output the specified byte stream Write a byte
Directly known subclass :ByteArrayOutputStream、FileOutputStream、FilterOutputStream ObjectOutputStream、OutputStream、PipeOutputStream
FileOutputStream File byte output stream effect : Write the data in memory to a file
Construction method :
FileOutputStream(String name) Create a file with the specified name Output file stream for writing data
FileOutputStream(File file) Create a File Object to write data in the file represented by File output stream
Parameters : The destination where the data is written
String name: The destination is the path of a file
File file : The destination is a file
The function of construction method :
- Create a FileOutputStream object
- According to the file passed in the constructor / File path , Create an empty file
- Will be able to FileOutputStream Object points to the created file
The principle of writing data :( Memory - Hard disk )
Java Program —>JVMàOS( operating system )àOS( Own method ) Call the method that writes the data à Write data to a file
The steps of using byte output stream :
- Create a FileOutputStream object , The destination to which the data is to be written in the constructor
- call FileOutputStream Object write Method to write data to a file
- Release resources ( Stream usage will occupy the memory of the hardtop , Empty the memory after use , Improve the efficiency of the program )
fos.write(97);// When writing data , Will be able to 10 Hexadecimal integer 97, Convert to binary integer
The data stored in the hard disk is bytes
Any text editor ( Notepad ) When you open the file again , Will look up the code table , Convert bytes to character representation
0-127 Inquire about ASCII surface
See the system default code table for other values ( Chinese system GBK)
Detailed code : Write data to computer memory E:\\ Multithreading \\a.txt
package demo27 Byte output stream writes data to file ;
/*
Construction method :
FileOutputStream(String name) Create an output file stream that writes data to a file with the specified name
FileOutputStream(File file) Create a File Object represents the file output stream in which data is written
Parameters : The destination where the data is written
String name: The destination is the path of a file
File file : The destination is a file
The function of construction method :
1. Create a FileOutputStream object
2. According to the file passed in the constructor / File path , Create an empty file
3. Will be able to FileOutputStream Object points to the created file
*/
import java.io.FileOutputStream;
import java.io.IOException;
public class DemoOutputStream {
public static void main(String[] args) throws IOException {
//1. Create a FileOutputStream object , The destination to which the data is to be written in the constructor
FileOutputStream fos=new FileOutputStream("E:\\ Multithreading \\a.txt");//FileOutputStream There are exceptions that need to be thrown
//2. call FileOutputStream Object write Method to write data to a file
fos.write(97);// When writing data , Will be able to 10 Hexadecimal integer 97, Convert to binary integer
//3. Release resources ( Stream usage will occupy the memory of the hardtop , Empty the memory after use , Improve the efficiency of the program )
fos.close();
}
}
result :
E:\\ At the beginning of multithreading :
After running the code :
边栏推荐
- 【云原生】3.1 Kubernetes平台安装KubeSpher
- 趋势前沿 | 达摩院语音 AI 最新技术大全
- 【LeetCode】18、四数之和
- MySQL advanced learning summary 9: create index, delete index, descending index, and hide index
- Vulhub vulnerability recurrence 72_ uWSGI
- [QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
- Compilation et connexion de shader dans games202 - webgl (comprendre la direction)
- Improve jpopup to realize dynamic control disable
- Set detailed map + interview questions
- Class inheritance in yyds dry inventory C
猜你喜欢
Sword finger offer II 039 Maximum rectangular area of histogram
Huawei equipment is configured with OSPF and BFD linkage
浅谈镜头滤镜的类型及作用
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Ad20 is set with through-hole direct connection copper sheet, and the bonding pad is cross connected
Class inheritance in yyds dry inventory C
[mask requirements of OSPF and Isis in multi access network]
【云原生】3.1 Kubernetes平台安装KubeSpher
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
用StopWatch 统计代码耗时
随机推荐
03. Login of development blog project
Unity gets the width and height of Sprite
[QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
Solution of QT TCP packet sticking
02. 开发博客项目之数据存储
Questions d'examen écrit classiques du pointeur
注释、接续、转义等符号
Mysql高级篇学习总结9:创建索引、删除索引、降序索引、隐藏索引
02. Develop data storage of blog project
UCF(暑期团队赛二)
Mongodb basic knowledge summary
01. Project introduction of blog development project
TCP three handshakes you need to know
2022半年总结
Select knowledge points of structure
Nacos TC setup of highly available Seata (02)
Some common skills on unity inspector are generally used for editor extension or others
Pointer classic written test questions
Note the various data set acquisition methods of jvxetable
flutter 实现一个有加载动画的按钮(loadingButton)