当前位置:网站首页>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
边栏推荐
- Learn about the Internet of things protocol WiFi ZigBee Bluetooth, etc. --- WiFi and WiFi protocols start from WiFi. What do we need to know about WiFi protocol itself?
- px em rem的区别
- left_ and_ right_ Net interpretable design
- C language exercises (recursion)
- Sword finger offer II 038 Daily temperature
- 2022.7.2-----leetcode.871
- 卸载Google Drive 硬盘-必须退出程序才能卸载
- lightroom 导入图片灰色/黑色矩形 多显示器
- 分布式CAP理论
- [openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
猜你喜欢
C realize Snake games
JS arguments parameter usage and explanation
Actual cases and optimization solutions of cloud native architecture
Yiwen unlocks Huawei's new cloud skills - the whole process of aiot development [device access - ESP end-to-side data collection [mqtt]- real time data analysis] (step-by-step screenshot is more detai
Grounding relay dd-1/60
检漏继电器JY82-2P
webrtc 快速搭建 视频通话 视频会议
Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout
Distributed cap theory
每周小结(*63):关于正能量
随机推荐
卸载Google Drive 硬盘-必须退出程序才能卸载
LayoutManager布局管理器:FlowLayout、BorderLayout、GridLayout、GridBagLayout、CardLayout、BoxLayout
Uninstall Google drive hard drive - you must exit the program to uninstall
js arguments参数使用和详解
How much computing power does transformer have
Accidentally deleted the data file of Clickhouse, can it be restored?
My NVIDIA developer journey - optimizing graphics card performance
Webrtc quickly set up video call and video conference
Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
How to determine whether an array contains an element
Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
检漏继电器JY82-2P
Detectron: train your own data set -- convert your own data format to coco format
[excel] PivotChart
ANSYS command
Learn about the Internet of things protocol WiFi ZigBee Bluetooth, etc. --- WiFi and WiFi protocols start from WiFi. What do we need to know about WiFi protocol itself?
JS get the attribute values nested in the object
Actual cases and optimization solutions of cloud native architecture
Wechat applet +php realizes authorized login
509. 斐波那契数、爬楼梯所有路径、爬楼梯最小花费