当前位置:网站首页>_ 19_ IO stream summary
_ 19_ IO stream summary
2022-06-25 16:20:00 【weixin_ forty-eight million six hundred and forty-four thousand】
1 IO
summary : A stream is an ordered set of bytes from beginning to end , Is the general term or abstraction of data transmission . 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
I : input Input
O : output Output
1.2 classification
It is divided into byte stream and character stream by data type
Different by data flow , It is divided into input stream and output stream .
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
Byte stream | Character stream | |
Input stream | InputStream | Reader |
Output stream | OutputStream | Writer |
1.3.1
InputStream
Common methods
void close() : Close this output stream and release all system resources that are not related to it
void flush() : Flushes this output stream and forces all buffered output bytes to be written
void write(byte[] b) : take b.length Bytes are written to this input stream from the specified byte array
void write(byte[] b,int off, int len) : Removes the specified byte array from the offset off At the beginning len Bytes are written to this input stream
abstract void write(int b) : Writes the specified bytes to this input stream
- Reader
- Writer
- File stream
- FileInputStream
- summary
- FileInputStream
- File stream
Used to open the file and read the data in the file
Want to read a file , You have to find it
1 Absolute position
The system root directory shall prevail , such as D:/xxx\\xxx\xx\a.txt
2 The relative position
./ Represents the current directory
../ Indicates the parent directory
../../ Go to the superior directory
Common methods
Read Use
public static void main(String[] args) {
// Create byte input stream
// Incoming file path , It is used to open this file to get data
// Absolute path
// FileInputStream fis = new FileInputStream("D:\\a.txt");
// stay eclipse in ,./ Locate the current project , Not the current file
// Relative paths
try {
FileInputStream fis = new FileInputStream("./src/b.txt");
// read : Read a byte , And return the corresponding ASCII Code value , Return to int type , If you reach the end of the file ( After reading the ) Then return to -1
// int data = fis.read();
int data = 0;
while ((data = fis.read()) != -1) {
System.out.print ((char) data);
}
// data = fis.read();
// while (data != -1) {
// System.out.print((char) data);
// data = fis.read();
// }
// close resource
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// Create byte input stream
FileInputStream fis = null;
try {
fis = new FileInputStream("./src/b.txt");
// read : Read a byte , And back to , Return to int type , If you reach the end of the file ( After reading the ) Then return to -1
// int data = fis.read();
int data = 0;
while ((data = fis.read()) != -1) {
System.out.print ((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
read : Read a byte , And return the corresponding ASCII Code value , Return to int type , If you reach the end of the file ( After reading the ) Then return to -1
public static void main(String[] args) {
try(FileInputStream fis = new FileInputStream("./src/b.txt")) {
// available : The number of bytes that can be read
System.out.println(fis.available());
// The larger the array capacity, the higher the efficiency , When the capacity and data size are just the same , Highest efficiency
byte[] bytes = new byte[fis.available()];
int temp = 0;
while (( temp = fis.read(bytes)) != -1) {
// Convert byte array to string , Data duplication may occur
// System.out.print(new String(bytes));
// because read Returns the number of elements currently read , So we can specify how much to read , How much to convert
// Array , The starting position ( contain ) , Number
System.out.print(new String(bytes,0,temp));
}
} catch (Exception e) {
e.printStackTrace();
}
}
Read Overload use
FileReader summary
FileReader : Read one character at a time , That's two bytes , It is mainly used to read plain text files , Solve the mess
*
* read() : Read one character at a time , Return the corresponding ASCII code , When the end of the file is reached, return -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
public static void main(String[] args) {
try (FileReader fr = new FileReader("./src/b.txt");){
int temp = 0;
while ((temp = fr.read()) != -1) {
System.out.print((char)temp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try (FileReader fr = new FileReader("./src/b.txt");){
int temp = 0;
char[] chars = new char[10];
while ((temp = fr.read(chars)) != -1) {
System.out.print(new String(chars,0,temp));
}
} catch (Exception e) {
e.printStackTrace();
}
}
- FileOutputStream
- summary
Common methods
Construction method
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream("D:/a.txt");) {
// Write the corresponding ASCII code
fos.write(97);
byte[] bytes = {97,98,99};
// Write out array
fos.write(bytes);
// You cannot write out a string directly
String str = " how are you ";
// getBytes : Convert the string to a byte array
fos.write(str.getBytes());
// Refresh cache , Force persistence
fos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
Usage mode
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);
}
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();
}
}
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();
}
}
BufferedWriter
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();
}
}
边栏推荐
- Advanced SQL statement 1 of Linux MySQL database
- 10款超牛Vim插件,爱不释手了
- Report on Hezhou air32f103cbt6 development board
- 合宙Air32F103CBT6開發板上手報告
- Cocoapods installation in 2021
- Coredata data persistence
- Power representation in go language
- Introduction to MgO 256gb NAND flash chip
- What can NFT metauniverse development do?
- Problems caused by using ApplicationContext to render layout
猜你喜欢
Gold three silver four, an article to solve the resume and interview
Principle analysis of ThreadLocal source code
赫尔辛基交通安全改善项目部署Velodyne Lidar智能基础设施解决方案
Mixed density network (MDN) for multiple regression explanation and code example
B站付费视频使up主掉粉过万
Alvaria announces Jeff cotten, a veteran of the customer experience industry, as its new CEO
Nsurlsession learning notes (III) download task
Navicat Premium 15 for Mac(数据库开发工具)中文版
GridLayout evenly allocate space
Why does golang's modification of slice data affect the data of other slices?
随机推荐
Deep learning pytorch cifar10 dataset training "suggestions collection"
Most commonly used SQL statements
商城风格也可以很多变,DIY了解一下!
Message format of Modbus (PLC)
js 给元素添加自定义属性
地理位置数据存储方案——Redis GEO
报错:homebrew-core is a shallow clone
What is session? How is it different from cookies?
教务系统开发(PHP+MySQL)
Check whether the port number is occupied
Educational administration system development (php+mysql)
Servlet details
Resolve the format conflict between formatted document and eslint
一文带你搞懂 JWT 常见概念 & 优缺点
Consumer and producer cases of inter thread synchronization (condition variable)
Why does golang's modification of slice data affect the data of other slices?
Classic deadlock scenario of multithreading and its solution (philosopher dining problem)
What plug-ins are available for vscade?
炮打司令部,别让一个UI框架把你毁了
有哪些新手程序员不知道的小技巧?