当前位置:网站首页>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
边栏推荐
- How to read the source code [debug and observe the source code]
- 【Proteus仿真】用24C04与1602LCD设计的简易加密电子密码锁
- Simulation scheduling problem of SystemVerilog (1)
- Record: install MySQL on ubuntu18.04
- SQL: special update operation
- How can I avoid "div/0!" Errors in Google Docs spreadsheet- How do I avoid the '#DIV/0!' error in Google docs spreadsheet?
- Differential constrained SPFA
- Succession of flutter
- 利用可视化结果,点击出现对应的句子
- Flutter network and data storage framework construction-b1
猜你喜欢

Read the paper glodyne global topology preserving dynamic network embedding

Using the visualization results, click to appear the corresponding sentence

Record the errors reported when running fluent in the simulator

Record: MySQL changes the time zone

Record: install MySQL on ubuntu18.04

Implementation of cqrs architecture mode under Kratos microservice framework

FBI 警告:有人利用 AI 换脸冒充他人身份进行远程面试
![[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]](/img/cb/ee696d5a7d6bef96fe0db89e8478ed.jpg)
[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]
![[leetcode] [SQL] notes](/img/8d/160a03b9176b8ccd8d52f59d4bb47f.png)
[leetcode] [SQL] notes

leetcode:11. 盛最多水的容器【双指针 + 贪心 + 去除最短板】
随机推荐
Record: pymysql is used in pycharm to connect to the database
组策略中开机脚本与登录脚本所使用的用户身份
leetcode:556. Next larger element III [simulation + change as little as possible]
KINGS
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
Leetcode: 11. Récipient contenant le plus d'eau [double pointeur + cupidité + enlèvement de la plaque la plus courte]
记录在模拟器中运行flutter时报的错
Max of PHP FPM_ Some misunderstandings of children
Briefly describe the quantitative analysis system of services
DriveSeg:动态驾驶场景分割数据集
我眼中真正优秀的CTO长啥样
Driveseg: dynamic driving scene segmentation data set
Ego planner code parsing Bspline_ Optimizer section (2)
[combinatorics] dislocation problem (recursive formula | general term formula | derivation process)*
Scrapy爬虫框架
math_泰勒公式
VLAN experiment
Shell script return value with which output
The installation path cannot be selected when installing MySQL 8.0.23