当前位置:网站首页>Inputstream/outputstream (input and output of file)
Inputstream/outputstream (input and output of file)
2022-07-04 06:10:00 【Slang H.】
List of articles
We have already talked about File Some common methods of class , For file systems , There must be input and output of file content
One 、InputStream
1. About file reading
There are two ways : Direct reading and text reading
No matter what kind of reading , All need to use java.io.InputStream
Indicates the input stream
InputStream Itself is an abstract class , What we use in real use is its implementation class FileInputStream
For the degree of the file , We can abstract it into getting water from the tap , We need to prepare a container , Go and get the water from the tap 
Be careful : There are two situations in water connection :1. There is water at present 2. There is no water at present , But there will be water behind 3. There is currently no water , There will be no water behind .
In the code , about 1. This situation , Directly return a number greater than zero , Indicates the current amount of water flowing about 2 Returns the 0, It means there is no water at present , But there will be about
3 Return to one -1, It means there is no water at present , There will be no more water after full .
Use InputStream when , We need to prepare a container in advance ( Usually a byte[])
1. Direct reading
Read as binary data , That's in the code byte In units of
When using read , We use InputStream Implementation class read() Method
such as : stay D:/IO There is a world.txt file ,
We need to read his content
a. Read one at a time 
b. Read more than one at a time 
Such a result is meaningless to us , So what we need is to read the text directly
2、 Text reading
The so-called text reading , The content we get is the original content of the document .
When reading, we should pay attention to the original encoding format of the file , And whether the codes when we escape are consistent , If it's not consistent , There may be garbled code , Problems such as unrecognized file content .
Common coding rules are 
On the basis of obtaining file data , We can use Scanner Class, the data we get , Escape according to the coding rules , Finally get the content of the source text , But the premise is that the coding rules we give should be consistent with the coding rules of the source text
For example, we need to read D:/IO In the catalog world.txt file 
We can see that his coding rule is :UTF-8
Next, write code to read the contents of this file
Use Scanner Construction method of class 
We can see , In this way, the content of the source text is read
Two 、OutputStream
There are also two cases for writing documents , Direct write , Write as original
When we write , Need to pass in the file path written , The file itself may not exist ( If it doesn't exist, it will be created automatically ), But his ancestral path must exist , when written , The original data in the file will be cleared
1、 Direct write
Direct write call write() When the method is used , You can only import byte perhaps byte[]
We're going straight to D:/IO/world.txt Write in file “a”
here world.txt Already exist 
Now let's write the code directly 
Such input must be very inconvenient
2、 Text writing
There are two common writing methods
All the way to world.txt write in “ Hello China ”

3、 buffer
Be careful : When the file is written , The reading and writing speed of memory is much higher than that of hard disk , therefore , There's a buffer , Data is written to the buffer first , When (1、 The buffer is full or reaches a certain threshold 2、 After a while 3、 The process takes the initiative to brush the disk ), The data in the buffer is written to the hard disk , To prevent the remaining data in the buffer from being brushed into the hard disk , Write the data at the end , We should call flush() Methods manually brush the disc
边栏推荐
- Understanding of cross domain and how to solve cross domain problems
- C realize Snake games
- 检漏继电器JY82-2P
- 509. Fibonacci number, all paths of climbing stairs, minimum cost of climbing stairs
- Win10 clear quick access - leave no trace
- 如何实现视频平台会员多账号登录
- Halcon image calibration enables subsequent image processing to become the same as the template image
- Recommended system 1 --- framework
- [excel] PivotChart
- How much computing power does transformer have
猜你喜欢

How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
![[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx](/img/a9/72791cbcc6c9da45e89450ab2820c1.jpg)
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx

740. Delete and get points

win10清除快速访问-不留下痕迹

Functions in C language (detailed explanation)

Compound nonlinear feedback control (2)

Leetcode question brushing record | 206_ Reverse linked list

配置交叉编译工具链和环境变量

Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.

体验碎周报第 102 期(2022.7.4)
随机推荐
Json Web token - jwt vs. Traditional session login Authentication
Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
ES6 模块化
Leakage detection relay jy82-2p
Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
740. Delete and get points
注释与注解
ES6 modularization
Understanding of cross domain and how to solve cross domain problems
[untitled]
Leetcode question brushing record | 206_ Reverse linked list
QT QTableWidget 表格列置顶需求的思路和代码
QT qtablewidget table column top requirements ideas and codes
C realize Snake games
One click filtering to select Baidu online disk files
How to clone objects
Take you to quickly learn how to use qsort and simulate qsort
如何展开Collapse 的所有折叠面板
Weekly summary (*63): about positive energy