当前位置:网站首页>_ 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();
}
}
边栏推荐
- GO语言-锁操作
- 深度学习 pytorch cifar10数据集训练「建议收藏」
- Multiple decorators decorate a function
- cmd。。。。。。
- 商城风格也可以很多变,DIY了解一下!
- 不要小看了积分商城,它的作用可以很大!
- The database records are read through the system time under the Android system, causing the problem of incomplete Reading Records!
- 心楼:华为运动健康的七年筑造之旅
- Most commonly used SQL statements
- Why does golang's modification of slice data affect the data of other slices?
猜你喜欢

一行代码可以做什么?

Don't underestimate the integral mall, its role can be great!

地理位置数据存储方案——Redis GEO

Helsinki traffic safety improvement project deploys velodyne lidar Intelligent Infrastructure Solution

What exactly is a handler

说下你对方法区演变过程和内部结构的理解

Alvaria宣布客户体验行业资深人士Jeff Cotten担任新首席执行官

Alvaria announces Jeff cotten, a veteran of the customer experience industry, as its new CEO

揭秘GaussDB(for Redis):全面对比Codis

Share the code technology points and software usage of socket multi client communication
随机推荐
10款超牛Vim插件,爱不释手了
面试官:你简历上说精通mysql,那你说下聚簇/联合/覆盖索引、回表、索引下推
Several ways of SQL optimization
Educational administration system development (php+mysql)
Problems caused by using ApplicationContext to render layout
XML usage and parsing of data storage and transmission files
Yadali brick playing game based on deep Q-learning
What is the NFT digital collection?
基于神经标签搜索,中科院&微软亚研零样本多语言抽取式摘要入选ACL 2022
What exactly is a handler
Don't underestimate the integral mall, its role can be great!
Resolve the format conflict between formatted document and eslint
One minute to familiarize yourself with the meaning of all fluent question marks
Sleep formula: how to cure bad sleep?
分享自己平时使用的socket多客户端通信的代码技术点和软件使用
Geographic location data storage scheme - redis Geo
This article will help you understand the common concepts, advantages and disadvantages of JWT
What can one line of code do?
The release of autok3s v0.5.0 continues to be simple and friendly
Navicat Premium 15 for Mac(数据库开发工具)中文版