当前位置:网站首页>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 :
边栏推荐
- Promotion hung up! The leader said it wasn't my poor skills
- 组播和广播的知识点梳理
- First acquaintance with CDN
- 指针经典笔试题
- js Array 列表 实战使用总结
- nacos-高可用seata之TC搭建(02)
- Easy to understand IIC protocol explanation
- Codeforces Round #804 (Div. 2) Editorial(A-B)
- 自建DNS服务器,客户端打开网页慢,解决办法
- HAC cluster modifying administrator user password
猜你喜欢
Vulhub vulnerability recurrence 73_ Webmin
Idea one key guide package
Unity Vector3. Use and calculation principle of reflect
Huawei equipment is configured with OSPF and BFD linkage
指针经典笔试题
[mask requirements of OSPF and Isis in multi access network]
04. Project blog log
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
05. Security of blog project
Pointer classic written test questions
随机推荐
Set detailed map + interview questions
Talking about the type and function of lens filter
C# AES对字符串进行加密
Nacos - TC Construction of High available seata (02)
Review of double pointer problems
备忘一下jvxetable的各种数据集获取方法
RT thread analysis log system RT_ Kprintf analysis
Leetcode dynamic planning day 16
[leetcode] 18. Sum of four numbers
【华为机试真题详解】统计射击比赛成绩
04. Project blog log
First acquaintance with CDN
Using stopwatch to count code time
EditorUtility. The role and application of setdirty in untiy
UCF (2022 summer team competition I)
Huawei equipment is configured with OSPF and BFD linkage
【torch】|torch.nn.utils.clip_grad_norm_
MySQL time processing
03. 开发博客项目之登录
HAC cluster modifying administrator user password