当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
ROS运行管理之launch文件
Unity 窗口界面的简单介绍
力扣题解 二叉树(6)
Pzk learns string function of C language (1)
力扣题解 二叉树(5)
[song] rebirth of me in py introductory training (10): numpy
wireshark数据包修改--IP地址修改(一)
允许或者禁止同时连接到一个non-domain和一个domain网络
Calculation of Huffman tree, code implementation and proof, graphic interpretation
[first song] Introduction to data science of rebirth -- return to advanced level
ROS分布式通信
Acwing the number of square arrays of one question per day
通信机制比较
C# Json编码在TCP通讯中的一些使用总结
Linear progression for face recognition
遥感影像识别进展2022/5/5
Thesis writing (harvest)
【第一篇博客-展望】
ULCL功能--5GC
Summary of the use of C # Jason code in TCP communication





![[first song] machine learning of rebirth - linear regression](/img/70/3efd9eacf88f55022eb52d096926f7.png)


