当前位置:网站首页>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.
边栏推荐
猜你喜欢

Lock wait timeout exceeded solution

MaxWell scraped data

ToDesk版本更新,引入RTC传输技术,是否早以替代向日葵远程控制?

Container sorting case

ISELED---氛围灯方案的新选择

This editor actually claims to be as fast as lightning!

【回归预测-lssvm分类】基于最小二乘支持向量机lssvm实现数据分类代码

极验深知v2分析

About the data synchronization delay of MySQL master-slave replication

Flink实时仓库-DWS层(关键词搜索分析-自定义函数,窗口操作,FlinkSql设置水位线,保存数据到Clickhouse)模板代码
随机推荐
MaxWell抓取数据
4 senior experts share the insider architecture design and implementation principles of Flink technology with years of experience in large factories
学习 MySQL 需要知道的 28 个小技巧
去腾讯面试,直接让人出门左拐 :幂等性都不知道!
【云原生 • DevOps】influxDB、cAdvisor、Grafana 工具使用详解
那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
1700. 无法吃午餐的学生数量
ROS 导航
Web3创始人和建设者必备指南:如何构建适合的社区?
LeetCode_数位枚举_困难_233.数字 1 的个数
数字量输入模块io
Metaverse Post Office AI space-themed series of digital collections will be launched at 10:00 on July 30th "Yuanyou Digital Collection"
GeoServer
Android jump to google app market
视频加密的误解
微服务该如何拆分?
组态 - 笔记
ECCV 2022 | Towards Data Efficient Transformer Object Detectors
Kubernetes应用管理深度剖析
CS内网横向移动 模拟渗透实操 超详细