当前位置:网站首页>Reading and writing of file content - data flow
Reading and writing of file content - data flow
2022-07-27 06:15:00 【Envy only mandarin ducks, not immortals】
Catalog
One 、 The reading and writing of content
4、 Specific introduction of the method
5、 utilize Scanner Read characters
One 、 The reading and writing of content
1、 About reading
① Direct reading ( Read as binary data , Expressed in code with byte In units of )
② Text reading
2、InputStream summary


InputStream Is from java.io.InputStream package , That is, the meaning of input stream , This class itself is an abstract class , The real use depends on the concrete implementation subclasses of this abstract class , such as FileInputStream, It's about the input stream of the file , Input stream can be abstracted as a tap to understand
3、FileInputStream summary

package data_flow;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
public class FileRead {
public static void main(String[] args) throws Exception {
//f1 It's the faucet
InputStream f1=new FileInputStream("D:\\ Desktop files \\java Course \\ Course materials \\javaweb\\a.txt");
// Prepare a bucket ,1024 Indicates the capacity of the barrel
byte[] bucket=new byte[1024];
// Use a bucket to catch water , Here n Indicates the actual water quantity received
int n=f1.read(bucket);
// When the reading is finished , Remember to turn off the tap ( Is to close the data flow )
System.out.println(n);
// View the data ( Decimal data )
byte[] bytes= Arrays.copyOf(bucket,n);
for (byte b : bytes) {
System.out.println(b);
}
f1.close();
}
}
4、 Specific introduction of the method
![]()
Take only a drop of water , What is returned is the data of the next byte , When the return value is -1 when , It means that there will never be water in the future
/**
* A drop of water reading
* @param f1
* @throws IOException
*/
public static void read1(InputStream f1) throws IOException {
while(true){
int data=f1.read();
if (data==-1){
// At this point, the data has been read
break;
}
// otherwise ,data Is the data stream
byte b=(byte) data;
System.out.println(b);
f1.close();
}
}
Connect a lot of water at one time , It can be stored in byte In an array , The return value is how much water we received this time , When n=-1 when , It means there will never be water in the future
/**
* Take a lot of water at one time
* @param f1
* @throws Exception
*/
public static void readbyte(InputStream f1) throws Exception{
byte[] bytes=new byte[5];
while (true){
int n=f1.read(bytes);
if (n==-1){
// It means that the data has been read
break;
}
for (int i = 0; i <n ; i++) {
byte b=bytes[i];
System.out.println(b);
}
}
f1.close();
}5、 utilize Scanner Read characters

6、OutputStream summary


import java.io.FileOutputStream;
import java.io.OutputStream;
public class OutputStream1 {
public static void main(String[] args) throws Exception {
// If the file does not exist before , It will be created
// If the file existed before , The previous content will be cleared and rewritten
try (OutputStream os = new FileOutputStream("D:\\ Desktop files \\java Course \\ Course materials \\javaweb\\a.txt")) {
//" I " Of UTF-8 code
os.write(0xe6);
os.write(0x88);
os.write(0x91);
// Make sure to write all the possible remaining data in the buffer to Output in
os.flush();
}
}
}
Use PrintWriter Method

import java.io.*;
public class PrintWritertest {
public static void main(String[] args) throws Exception {
try(OutputStream os=new FileOutputStream("D:\\ Desktop files \\java Course \\ Course materials \\javaweb\\a.txt")){
try(Writer writer=new OutputStreamWriter(os,"UTF-8")){
try(PrintWriter p1=new PrintWriter(writer)){
p1.println(1+1);
p1.print(3);
p1.printf("%s"+"%d"," I ",3);
p1.flush();
}
}
}
}
}
Two 、 Related codes
边栏推荐
猜你喜欢
随机推荐
IP核之RAM
一张图看懂指针
What is the difference between single line and three line when renting servers in Hong Kong?
Linked list palindrome judgment
[headline] Rebirth: the basis of CNN image classification
编程学习记录——第9课【操作符】
软件测试基础概念篇
C thread lock
[song] rebirth of me in py introductory training (8): module
Man moon myth reading notes
ROS节点名称重名
力扣题解 动态规划(5)
这是我的博客
1 semi automatic crawler
如何选择正确的服务器备份方法
ROS分布式通信
力扣每日一题 剑指 Offer II 091. 粉刷房子
SQL novice
C#线程锁(Lock)
Acwing the number of square arrays of one question per day
![[5.20 special] MATLAB, I'm confessing to you](/img/ce/ea8697db3e10a216efc9ac5e0fad8d.png)






![[Arduino] reborn Arduino monk (1)](/img/ce/67118ebd430a0ff3ef98ec8524ee4a.jpg)