当前位置:网站首页>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
边栏推荐
- Hard disk monitoring and analysis tool: smartctl
- 我眼中真正优秀的CTO长啥样
- Implementation of cqrs architecture mode under Kratos microservice framework
- EGO Planner代码解析bspline_optimizer部分(1)
- Day-27 database
- Help change the socket position of PCB part
- FBI warning: some people use AI to disguise themselves as others for remote interview
- 东数西算拉动千亿产业,敢啃“硬骨头”的存储厂商才更有机会
- [proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD
- 组策略中开机脚本与登录脚本所使用的用户身份
猜你喜欢

Zhengda futures news: soaring oil prices may continue to push up global inflation
![Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]. StandardContext](/img/56/ea61359dd149a49589ba7ad70812a0.jpg)
Failed to start component [StandardEngine[Catalina]. StandardHost[localhost]. StandardContext

VLAN experiment

Flutter网络和数据存储框架搭建 -b1

235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】

KINGS

SSM integration - joint debugging of front and rear protocols (list function, add function, add function status processing, modify function, delete function)

我們做了一個智能零售結算平臺

Chisel tutorial - 06 Phased summary: implement an FIR filter (chisel implements 4-bit FIR filter and parameterized FIR filter)

Su embedded training - Day10
随机推荐
Ctrip will implement a 3+2 work system in March, with 3 days on duty and 2 days at home every week
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
Dart JSON编码器和解码器剖析
How to design a high concurrency system
PyTorch中在反向传播前为什么要手动将梯度清零?
The more you talk, the more your stupidity will be exposed.
Smart wax therapy machine based on STM32 and smart cloud
Analysis of dart JSON encoder and decoder
SSH 远程执行命令简介
Ego planner code parsing Bspline_ Optimizer section (2)
Help change the socket position of PCB part
Unity2018 to wechat games without pictures
Work Measurement - 1
ActiveMQ的基础
High concurrency architecture cache
Dynamic planning -- expansion topics
235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】
Sustainable service business models
Simulation scheduling problem of SystemVerilog (1)
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】