当前位置:网站首页>Day_ 18 IO stream system
Day_ 18 IO stream system
2022-07-03 19:04:00 【Love Siyi alone】
BufferedInputStream/BufferedOutputStream
BufferedInputStream/BufferedOutputStream : Efficient byte input stream / Efficient byte output stream -> Buffer flow
Open a in memory buffer , The data will be stored in the buffer first , When the buffer is full, it will be automatically refreshed to the target file
// This convection is only a little different when creating the flow object , The logic of reading and writing is the same !!
Construction method :
BufferedInputStream(InputStream in)
new BufferedInputStream(new FileInputStream(" Source file address "));// Packaging flow
BufferedOutputStream(OutputStream out)
new BufferedOutputStream(new FileOutputStream(" Destination file address "));// Packaging flow
Overview of character stream
There are a lot of character files in the computer , It is inconvenient to operate character files with byte stream !
Character file : It works windows The built-in Notepad doesn't open " The statement " The file of
Root node of character stream : Reader/Writer -> abstract class
Normal character stream : FileReader/FileWriter
Efficient character stream : BufferedReader/BufferedWriter
FileReader/FileWriter
Construction method :
FileReader -> FileReader(" Source file address ");
FileWriter -> FileWriter(" Destination file address ");/FileWriter(" Destination file address ", Add write switch );
How to read and write :
FileReader Read method :
* int read(): Read one character at a time
* int read(char[] chs) : Read one array at a time
int read(char[] chs,int offset,int length) : Read part of an array one at a time
FileWriter The method of writing :
*void write(int ch): Write one character at a time
void write(char[] chs): Write an array of characters one at a time
*void write(char[] chs,int offset,int length): Write part of a character array one at a time
*void write(String str): Write one string at a time
void write(String str,,int offset,int length): Write one part of a string at a time
BufferedReader/BufferedWriter
BufferedReader/BufferedWriter : Efficient character input stream / Efficient character output stream -> Buffer flow , Packaging flow
Construction method :
BufferedReader(Reader in)
new BufferedReader(new FileReader(" Source file address "));// Packaging flow
BufferedWriter(Writer out)
new BufferedWriter(new FileWriter(" Destination file address "));// Packaging flow
The basic reading and writing methods are the same as ordinary character streams !
BufferedReader/BufferedWriter The unique method of ( important )
BufferedReader Special reading methods :
String readLine() : Read one line of string at a time // But don't read the newline at the end of a line
To make up for it BufferedReader Can't read line breaks , BufferedWriter You need to write a line and a newline
// Because of the different operating systems , Line breaks are different : windows:\r\n Linux:\n MacOS: \r
BufferedWriter Provides a way to write line breaks according to different operating systems : void newLine()
Encoding and decoding
Everything in the computer is bytes ( Everything in the computer is bit (0,1 Binary system ))
code : Write binary code
written words -- Coding format --> Binary code
decode : Parse binary code
Binary code -- Coding format --> written words
Why are files garbled : The encoding and decoding methods are different !!
Solve the mess : Ensure that the encoding format used in encoding and decoding is consistent !!
Ways to recommend : Manually change the encoding format of the document
Don't change the coding format of the platform
Try not to change with code
Encoding and decoding methods in strings
String --> Binary code (byte[]) ( code )
byte[] getBytes(): Encode the string content according to the default encoding format of the platform (byte[])
byte[] getBytes(String charsetName): Encode the string contents according to the specified encoding format (byte[])
Binary code --> String ( decode )
String(byte[] bytes) : Decode the byte array according to the default encoding format of the platform
String(byte[] bytes, int offset, int length) : Decode part of the contents of the byte array according to the default encoding format of the platform
// Decode the byte array according to the specified encoding format !
String(byte[] bytes, String charsetName)
String(byte[] bytes, int offset, int length, String charsetName)
Character set
Character set Is the general name of coding format ( There are multiple coding tables in a character set )
The most basic coding table : ASCII surface -> A character takes up a byte
Common character sets :
GB series : Chinese character set
GB2312
GBK: The current popular Chinese coding table -> A Chinese character takes up two bytes
Unicode : The standard character set on the Internet
UTF-8 : Internet default coding table -> One Chinese character takes up three bytes
UTF-16
UTF-32
Encoding table : ISO-8859-1 Latin code
InputStreamReader/OutputStreamWriter
InputStreamReader/OutputStreamWriter : Converted flow
characteristic :
1. A bridge between byte flow and character flow
2. Set the time for reading and writing Coding format -> The file is read and written according to the coding format !!
Construction method :
InputStreamReader(InputStream in) : Transfer byte input into character input stream , According to the default encoding format of the platform ;
OutputStreamWriter(OutputStream out) : Transfer byte output to character output stream , According to the default encoding format of the platform ;
--------------------------------------
// Character stream = Byte stream + Coding format ;
InputStreamReader(InputStream in, String charsetName) : According to the specified encoding format ;
OutputStreamWriter(OutputStream out, String charsetName) : According to the specified encoding format ;
Reading and writing methods : InputStreamReader/OutputStreamWriter yes Reader/Writer Subclasses of
read : Three ways of reading
Write : Five ways of writing
System.in/System.out
System Class :
System.in: Standard system input -> Byte stream (InputStream) -> Only do keyboard entry Source : keyboard
System.out: Standard system output -> Byte stream (PrintStream) -> Only do print console The goal is : Console
ObjectInputStream/ObjectOutputStream
serialize : Permanent storage
ObjectInputStream/ObjectOutputStream : Deserialize stream / Serialization flow ( Object input stream / Object output stream )
Construction method : Packaging flow
ObjectInputStream(InputStream in)
ObjectOutputStream(OutputStream out)
Conventional reading and writing methods : Like all byte streams
Three ways of reading , Three ways of writing
Special reading and writing methods : Read object / Write about
ObjectInputStream : Object readObject(): Read an object from the object file
ObjectOutputStream : void writeObject(Object obj): Write an object to the serialization file
matters needing attention :
1. If you want to write objects to a file , The class of the object must be required to implement Serializable Interface !!
2. The rule is : Load all objects into the set before writing objects Write the collection object to the file Write once !!
Set automatic generation VersionUID
Standard procedure for customizing description classes ( a key )
1 Private all member variables
2 Automatically generate parameterless constructs
3 Automatically generate all parameter constructs
4 Automatic generation getter and setter Method
5 Automatic generation equals and hashCode Method
6 Automatic generation toString Method
7 implements Serializable Serializable
8 Automatically generate serialization id
// Behavior of objects Write on demand
边栏推荐
- SSM整合-前后台协议联调(列表功能、添加功能、添加功能状态处理、修改功能、删除功能)
- Ego planner code parsing Bspline_ Optimizer section (3)
- Le changement est un thème éternel
- Leetcode: 11. Récipient contenant le plus d'eau [double pointeur + cupidité + enlèvement de la plaque la plus courte]
- There are several levels of personal income tax
- Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]. StandardContext
- 平淡的生活里除了有扎破皮肤的刺,还有那些原本让你魂牵梦绕的诗与远方
- [optics] vortex generation based on MATLAB [including Matlab source code 1927]
- Ego planner code parsing Bspline_ Optimizer section (1)
- leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】
猜你喜欢
Integrated easy to pay secondary domain name distribution system
Driveseg: dynamic driving scene segmentation data set
[leetcode weekly race] game 300 - 6110 Number of incremental paths in the grid graph - difficult
2022.02.11
Foundation of ActiveMQ
Ping problem between virtual machine and development board
【水质预测】基于matlab模糊神经网络水质预测【含Matlab源码 1923期】
Su embedded training - Day10
Record the errors reported when running fluent in the simulator
Zhengda futures news: soaring oil prices may continue to push up global inflation
随机推荐
[leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
A green plug-in that allows you to stay focused, live and work hard
Shell script return value with which output
Caddy server agent
Ego planner code parsing Bspline_ Optimizer section (3)
[mathematical modeling] ship three degree of freedom MMG model based on MATLAB [including Matlab source code 1925]
After nohup NPM start &, close the shell window directly, and the process closes accordingly
[Yu Yue education] world reference materials of Microbiology in Shanghai Jiaotong University
application
Database creation, addition, deletion, modification and query
Scrapy爬虫框架
利用可视化结果,点击出现对应的句子
php-fpm的max_chindren的一些误区
Pan for in-depth understanding of the attention mechanism in CV
Ego planner code parsing Bspline_ Optimizer section (2)
235. The nearest common ancestor of the binary search tree [LCA template + same search path]
High concurrency architecture cache
虚拟机和开发板互Ping问题
Simulation scheduling problem of SystemVerilog (1)
Record the errors reported when running fluent in the simulator