当前位置:网站首页>Use of InputStream and OutputStream
Use of InputStream and OutputStream
2022-07-30 15:32:00 【HUAWEI CLOUD】
1.Introduction to InputStream
java.io.InputStream is an abstract class of byte input stream, representing the top level of all classes of byte input streamfather.Used to read the data in the file into the java program.If you need to use the input stream, you must create its subclass to use it.When reading a file, we often use the FileInputStream file byte input stream, a subclass of the InputStream abstract class, to read the data in the file, convert the file in the disk into the form of a stream, and read it into the memory for use.
2.The use of FileInputStream
The construction methods of FileInputStream are: FileInputStream(File file) parameter is to pass a file of type File; FileInputStream(String name) parameter is to pass a file path of type String.
Common methods are:
int read() is to read one byte of data from the file, and return the read byte, if the read ends, return -1;
int read(byte[] b) is to read a byte array at a time, the input stream will put the read content into this byte array, and return the number of read, if readWhen the fetch is over, it will return -1;
void close() is to close the stream. If the stream is not closed, it will cause memory overflow, so after the end of use, you need to manually call the method to close the stream to close it.
3.FileInputStream to read the file
3.1 Create a FileInputStream stream object and specify the data source file to be read. At this time, if the specified data source file does not exist, a File not fount excep exception will be thrown.
3.2 Call the read() method to read data.
3.3 Assign the read byte to the variable i, and judge whether i is not equal to -1. If it is not -1, it means that it has not been read and is still reading. After the content is read this time, it will continue to circulate.Process the read content.
3.4 Call the close() method to close the stream to release resources.
The first one is to read one byte by one byte. This method cannot read Chinese, because one Chinese occupies multiple bytes. At this time, reading one byte at a time will split the Chinese for reading.
public class Test { public static void main(String[] args) throws IOException { FileInputStream stream = new FileInputStream("F:\\demo123\\test\\test.txt"); int i = 0; while ((i = stream.read()) != -1) { System.out.print((char) i); } stream.close(); }}Second, use the byte input stream to read a byte array at a time, the input stream will put the read content into the byte array, and return the number of read, if readReturn -1 at the end, which is much more efficient than the previous reading method.
public class Test1 { public static void main(String[] args) throws IOException { FileInputStream stream = new FileInputStream("F:\\test123\\test\\test.txt"); byte arr[]=new byte[1024]; int len = 0;//Return the number read//Use a loop to start reading while((len=stream.read(arr))!=-1){ System.out.println(new String(arr,0,len)); } stream.close(); }}4.FileOutputStream output stream for file copying
The process of FileOutputStream file copying is to read and write at the same time, and write the read content to the file each time it is read.Copying is done in the same way as above for reading one byte array at a time.
4.1 Create a FileInputStream stream object and specify the data source file to be read. At this time, if the specified data source file does not exist, a File not fount excep exception will be thrown.
4.2 Create a byte output stream object for writing, if not, it will be automatically created.
4.3 Create an array to read
4.4 Read the array from the file with the byte array, store it in the byte array, and write the read content to the destination file every time a content is read
public class Test2 { public static void main(String[] args) throws IOException { FileInputStream in= new FileInputStream("F:\\test123\\test\\test.txt"); FileOutputStream out = new FileOutputStream("F:\\test123\\test\\test1.txt"); byte arr[] = new byte[1024]; int len = 0; while ((len = in.read(arr)) != -1) { fs.write(arr,0,len); } in.close(); out.close(); }}The above is a summary of the simple usage of InputStream and FileOutputStream.
边栏推荐
- 如何做好技术选型
- Start learning C language
- This editor actually claims to be as fast as lightning!
- MongoDB启动报错 Process: 29784 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)
- Office Automation | Office Software and Edraw MindMaster Shortcuts
- 面试何惧调优!腾讯技术官私藏的性能优化方案手册,原理实战齐全
- 阿里CTO程立:阿里巴巴的开源历程、理念和实践
- What is the relationship between the construction of smart cities and 5G technology in the new era
- canal抓取数据
- [机缘参悟-53]:《素书》-3-修身养志[求人之志章第三]
猜你喜欢

Mysql数据库查询好慢,除了索引,还能因为什么?

Excel使用Visual Basic Editor对宏进行修改

Flink实时仓库-DWS层(关键词搜索分析-自定义函数,窗口操作,FlinkSql设置水位线,保存数据到Clickhouse)模板代码

Sentinel

JVM性能调优

极验深知v2分析

Mac 中 MySQL 的安装与卸载

Metaverse Post Office AI space-themed series of digital collections will be launched at 10:00 on July 30th "Yuanyou Digital Collection"

Flink real-time data warehouse completed

使用 protobuf 进行数据序列化
随机推荐
如何做好技术选型
瑞吉外卖项目实战Day02
学习 MySQL 需要知道的 28 个小技巧
1700. 无法吃午餐的学生数量
GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?
双碳目标下:农田温室气体排放模拟
Mac 中 MySQL 的安装与卸载
Sentinel
Use of SLF4J
使用 protobuf 进行数据序列化
Fink异步IO的实战(关联维表)
JUC common thread pool source learning 02 ( ThreadPoolExecutor thread pool )
Meta首份元宇宙白皮书9大看点,瞄准80万亿美元市场
MongoDB starts an error Process: 29784 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)
Start learning C language
canal抓取数据
【回归预测-lssvm分类】基于最小二乘支持向量机lssvm实现数据分类代码
MySql报错:SqlError(Unable to execute query“, “Can‘t create/write to file OS errno 2 - No such file...
MongoDB启动报错 Process: 29784 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)
MySql error: SqlError(Unable to execute query", "Can't create/write to file OS errno 2 - No such file...