当前位置:网站首页>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 :



边栏推荐
- Huawei od computer test question 2
- [QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
- 03. 开发博客项目之登录
- 剑指 Offer II 039. 直方图最大矩形面积
- LeetCode_字符串反转_简单_557. 反转字符串中的单词 III
- Self built DNS server, the client opens the web page slowly, the solution
- Please wait while Jenkins is getting ready to work
- Codeforces Round #804 (Div. 2) Editorial(A-B)
- Modbus protocol communication exception
- 无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
猜你喜欢

Codeforces Round #804 (Div. 2) Editorial(A-B)

图数据库ONgDB Release v-1.0.3

JS array list actual use summary

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

Nacos - TC Construction of High available seata (02)

Ora-01779: the column corresponding to the non key value saving table cannot be modified

【torch】|torch. nn. utils. clip_ grad_ norm_

Excel转换为Lua的配置文件

Vulhub vulnerability recurrence 71_ Unomi

【云原生】3.1 Kubernetes平台安装KubeSpher
随机推荐
Microblogging hot search stock selection strategy
Imperial cms7.5 imitation "D9 download station" software application download website source code
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
[detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
Sliding window problem review
Talking about the type and function of lens filter
Figure database ongdb release v-1.0.3
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
Steady, 35K, byte business data analysis post
Class inheritance in yyds dry inventory C
Questions d'examen écrit classiques du pointeur
02. Develop data storage of blog project
Summary of redis basic knowledge points
Algorithm -- climbing stairs (kotlin)
Solution of QT TCP packet sticking
Oracle deletes duplicate data, leaving only one
Ora-01779: the column corresponding to the non key value saving table cannot be modified
ByteDance program yuan teaches you how to brush algorithm questions: I'm not afraid of the interviewer tearing the code
[untitled]
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