当前位置:网站首页>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.
边栏推荐
- Allure Advanced - Dynamically Generate Report Content
- 分布式限流 redission RRateLimiter 的使用及原理
- 《二舅》刷屏了!
- MySql error: SqlError(Unable to execute query", "Can't create/write to file OS errno 2 - No such file...
- Understand the Chisel language. 29. Chisel advanced communication state machine (1) - communication state machine: take the flash as an example
- GeoServer + openlayers
- 延时消息队列
- 使用 protobuf 进行数据序列化
- Flink实时仓库-DWS层(关键词搜索分析-自定义函数,窗口操作,FlinkSql设置水位线,保存数据到Clickhouse)模板代码
- SEATA分布式事务
猜你喜欢
随机推荐
有关收集箱的改进建议
Kubernetes应用管理深度剖析
About the data synchronization delay of MySQL master-slave replication
极验深知v2分析
Mac 中 MySQL 的安装与卸载
Lock wait timeout exceeded solution
GeoServer
Normal and escaped strings for postgresql
Android jump to google app market
本地事务与分布式事务
延时消息队列
微服务该如何拆分?
剑指 Offer II 037. 小行星碰撞
[Cloud native] Alibaba Cloud ARMS business real-time monitoring
异常情况处置方案
Flink优化
去腾讯面试,直接让人出门左拐 :幂等性都不知道!
Flink实时仓库-DWS层(状态编程,windowall的使用,数据保存到clickhouse)模板代码
TensorFlow自定义训练函数
Huawei issues another summoning order for "Genius Boys"!He, who had given up an annual salary of 3.6 million, also made his debut









