当前位置:网站首页>Day-19 IO stream
Day-19 IO stream
2022-07-07 12:36:00 【Xiaobai shelter】
1.IO
1.1 summary
Flow is a set of sequential , Set of bytes with start and end points , Is the general term or abstraction of data transmission . That is, the transmission of data between two devices is called streaming , The essence of stream is data transmission , According to the characteristics of data transmission, streams are abstracted into various kinds , Convenient and more intuitive data operation .
I:input Input stream
O:output: Output stream
1.2 classification
According to the type of data processed , It is divided into byte stream and character stream
It is divided into input flow and output flow according to different data flow directions .( In and out are relative to memory )
According to different functions , It is divided into node flow and processing flow
Node flow : Direct manipulation of data sources
Processing flow : Process other streams
1.3 Four abstract classes
This table example : Byte input stream : InputStream
1.4 File stream
1.4.1 FileInputStream
summary : Used to open the file and read the data in the file
Want to read a file , You must find it first , You must use this file stream
1. Absolute position
The system root directory shall prevail , such as D:/xxx/xxx/xxx/a.txt
2. The relative position
./ Represents the current directory
…/ Indicates the parent directory
…/…/ Go to the superior directory
1.4.2 Read Use
read: Read a byte , And return the corresponding ASCLL Code value , Return to int type , If you reach the end of the file ( After reading the ) Then return to -1
read Method overloading : You can pass an array , One read will fill the array / After reading , And then back in one go
return int type , Is the current number of reads , If the end of the file is reached return -1
The array is equivalent to a buffer , It will improve efficiency
1.4.3 FileReader
summary : FileReader: Read one character at a time , That's two bytes , Mainly used to read plain text , Solve the mess
read(): Read one character at a time , Return the corresponding ASCLL code , Return at the end -1
read(char[]): Read one character array at a time , Improve reading efficiency , Returns the number of characters read locally , Return at the end of the file -1
Usage mode
stay public class IO_05_FileReader {
public static void main(String args[]){
try(FileReader fr=new FileReader("./src/IO/b.txt");) {
int temp=0;
while((temp=fr.read())!=-1){
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} Insert the code chip here
1.4.4 FileOutputStream
summary
FileOutputStream It's a byte output stream , Used to write data out to a specified file
If the specified file does not exist , The file will be created automatically , But it doesn't create a directory ( No folders will be created )
Construction method
public static void main(String[] args) throws IOException {
// Overwrite write , When creating the output stream object of the file , The contents of the file will be cleared
// FileOutputStream fos1 = new FileOutputStream("D:/a.txt");
// When you create an object , Pass in the second parameter true Description is append write , The file will not be emptied
FileOutputStream fos1 = new FileOutputStream("D:/a.txt",true);
fos1.write(98);
}
}
Usage mode
1.4.6 FileWriter
public static void main(String[] args) {
// Usage and byte output Agreement It's just a new method overload that can write out strings
try (FileWriter fw = new FileWriter("D:/a.txt")){
// You can write the data in the character array each time
char[] chars = {
'a','c'};
fw.write(chars);
// You can write out the string directly
fw.write(" how are you ?");
fw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
1.4.7 Buffer flow
characteristic :
① It exists mainly to improve efficiency , Reduce the number of physical reads
② Provide readLine()、newLine() Such a convenient way ( For buffered character stream )
③ When reading and writing , There will be a cache part , call flush To refresh the cache , Write memory data to disk
BufferedReader
public static void main(String[] args) {
try (
// Create character input stream
FileReader fr = new FileReader("./src/day_01/Test_01.java");
// Create character input buffer stream object
BufferedReader br = new BufferedReader(fr);) {
// br.readLine() : Read a row of data , Return the contents of the read row of data , by String , Return at the end of the file null
String temp = null;
while ((temp = br.readLine()) != null) {
System.out.println(temp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
BufferWriter
public static void main(String[] args) {
try (
// Create character output stream
FileWriter fw = new FileWriter("D:/a.txt");
// Create character output buffer stream
BufferedWriter bw = new BufferedWriter(fw);) {
// Write
bw.write(" how are you ");
// Line break
bw.newLine();
bw.write(" Can you manage it ");
// Brush cache
bw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
1.4.8 Converted flow
characteristic :
Conversion stream refers to the conversion of bytes to character stream , There are mainly InputStreamReader and OutputStreamWriter
InputStreamReader It is mainly about converting byte input stream into character input stream
OutputStreamWriter It mainly converts byte output stream into character output stream
1.7 Print stream
Print stream is the most convenient class for output
Include byte print stream PrintStream, Character print stream PrintWriter
PrintStream yes OutputStream Subclasses of , After passing an instance of the output stream to the print stream , It is more convenient to output, which is equivalent to repacking the output stream
PrintStream Class print() Class methods are overloaded many times print(int i)、print(boolean b)、print(char c)
public static void main(String[] args) throws Exception{
// Create output stream
FileOutputStream fos = new FileOutputStream("D:/a.txt");
// Encapsulated as a print stream
PrintStream ps= new PrintStream(fos);
ps.println(123);
ps.println("asdasd Assad ");
// System Print stream in Print to console by default
System.out.println("xxxx");
// change System Print stream in
System.setOut(ps);
// All the following printing operations , No longer displayed in the console , Instead, print to the specified file
System.out.println("xxxxxxxxx");
System.out.println(" how are you ");
}
1.8 Data flow
summary
Data flow It is for the unity of data reading between different platforms ,Linux,Windows When the operating system stores data, the way is different
To address the differences between ,java Provides two lightweight methods , As long as each platform has java Environmental Science , Can ensure the consistency of data
such as During network protocol transmission , It is very applicable
Use
public class IO_14_DataOutputStream {
public static void main(String[] args) throws Exception{
// Create objects
DataOutputStream dos = new DataOutputStream(new FileOutputStream("D:/a.txt"));
// Writing data
dos.writeByte(1);
dos.writeShort(12);
dos.writeInt(51);
dos.writeUTF(" Assad Ji'an City ");
dos.flush();
dos.close();
}
边栏推荐
- Attack and defense world - PWN learning notes
- 跨域问题解决方案
- Connect to blog method, overload, recursion
- SQL blind injection (WEB penetration)
- [deep learning] image multi label classification task, Baidu paddleclas
- ps链接图层的使用方法和快捷键,ps图层链接怎么做的
- 【统计学习方法】学习笔记——第五章:决策树
- SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
- Static routing assignment of network reachable and telent connections
- Is it safe to open an account in Ping An Securities mobile bank?
猜你喜欢
消息队列消息丢失和消息重复发送的处理策略
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Attack and defense world ----- summary of web knowledge points
EPP+DIS学习之路(2)——Blink!闪烁!
[play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
About sqli lab less-15 using or instead of and parsing
MPLS experiment
数据库系统原理与应用教程(009)—— 概念模型与数据模型
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
SQL Lab (46~53) (continuous update later) order by injection
随机推荐
TypeScript 接口继承
Static routing assignment of network reachable and telent connections
AirServer自动接收多画面投屏或者跨设备投屏
Vxlan static centralized gateway
leetcode刷题:二叉树26(二叉搜索树中的插入操作)
Aike AI frontier promotion (7.7)
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
数据库系统原理与应用教程(008)—— 数据库相关概念练习题
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
爱可可AI前沿推介(7.7)
[pytorch practice] image description -- let neural network read pictures and tell stories
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
NGUI-UILabel
Typescript interface inheritance
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
On valuation model (II): PE index II - PE band
Inverted index of ES underlying principle
Realize a simple version of array by yourself from
Apache installation problem: configure: error: APR not found Please read the documentation
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed