IO in Java
- IO refers to the input and output of data streams. IO streams are mainly divided into two categories, byte streams and character streams . The byte stream can handle any type of data (pictures, videos, etc.), and the character stream can only handle character data.
- IO is divided into blocking IO and non-blocking NIO
Concept and function
- A stream is a set of sequential start and end points, which is a general term or abstraction for data transmission.
- The essence of streaming is data transmission.
Java flow class diagram structure
Flow classification
Character stream and byte stream
- Character stream: When reading the byte stream, check the specified code table.
- The byte stream is in bytes, and the character stream is in characters.
- The byte stream can handle any type of data, and the character stream can only handle character type data.
Input stream and output stream
- The input stream can only be read.
- The output stream can only be written.
RandomAccessFile type
This object is not a member of the stream system. It encapsulates the byte stream and at the same time encapsulates a buffer (character array), and manipulates the data in the character array through an internal pointer. Features of the object:
-
This object can only manipulate files, so the constructor receives two types of parameters: a. String file path; b. File object.
-
The object can both read and write files, and specify the operation mode (r, rw) when instantiating the object
File operations Utils
- FileUtils.java
package utils;
import java.io.*;
import java.util.logging.Logger;
/**
* @Description:
* @Author:LiDong
* @Create:2021/1/4
* @Version:1.0.0
*/
public class FileUtils {
private static final Logger logger = Logger.getLogger(String.valueOf(FileUtils.class));
/**
* @return
* @Author LiDong
* @Description //RandomAccessFile
* @Date 20:48 2021/1/4
* @Param
**/
public static String toStringByRandomAccessFile(File file, String modle) throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
StringBuffer stringBuffer = new StringBuffer();
byte bit[] = new byte[1024];
int hasRead = 0;
/**
while ((hasRead=randomAccessFile.read(bit))>0) {
stringBuffer.append(new String(bit, 0, hasRead));
}*/
while (randomAccessFile.read(bit) != -1) {
stringBuffer.append(new String(bit));
}
randomAccessFile.close();
return String.valueOf(stringBuffer);
}
/**
* @return void
* @Author LiDong
* @Description //文件尾部追加内容
* @Date 21:20 2021/1/4
* @Param [file, res]
**/
public static void writeByRandomAccessFile(File file, String res) throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
// 记录文件长度,设置从文件末尾处写入内容
randomAccessFile.seek(randomAccessFile.length());
randomAccessFile.write(res.getBytes());
randomAccessFile.close();
}
/**
* @return java.lang.String
* @Author LiDong
* @Description //FileReader 读取文件中的内容:字符流
* @Date 21:28 2021/1/4
* @Param [file]
**/
public static String readByFileReader(File file) throws IOException {
StringBuffer stringBuffer = new StringBuffer();
// 创建FileReader对象
FileReader fileReader = new FileReader(file);
// 读取位置
int head = 0;
while ((head = fileReader.read()) != -1) {
// 将读取的内容强制转换为char类型
stringBuffer.append((char) head);
}
fileReader.close();
return String.valueOf(stringBuffer);
}
/**
* @return void
* @Author LiDong
* @Description //filewriter 字符流方式写文件
* @Date 21:37 2021/1/4
* @Param [file, res]
**/
public static void writeByFileWriter(File file, String res) throws IOException {
// FileWriter fileWriter = new FileWriter(file);
// 追加的方式写入内容
FileWriter fileWriter = new FileWriter(file, true);
fileWriter.write(res);
fileWriter.close();
}
/**
* @return java.lang.String
* @Author LiDong
* @Description //字符缓冲区输入流读文件
* @Date 21:43 2021/1/4
* @Param [file]
**/
public static String readByBufferedReader(File file) throws IOException {
StringBuffer stringBuffer = new StringBuffer();
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String strLine = "";
while ((strLine = bufferedReader.readLine()) != null) {
stringBuffer.append(strLine);
}
fileReader.close();
bufferedReader.close();
return String.valueOf(stringBuffer);
}
/**
* @Author LiDong
* @Description // FileInputStream 读文件
* @Date 21:53 2021/1/4
* @Param [file]
* @return java.lang.String
**/
public static String readByFileInputStream(File file) throws IOException {
StringBuffer stringBuffer = new StringBuffer();
//创建流对象
FileInputStream fileInputStream = new FileInputStream(file);
int data;
//available() 可读取的字节数
logger.info("可读取的字节数:" + fileInputStream.available());
//循环读取内容
while ((data = fileInputStream.read()) != -1) {
stringBuffer.append((char)data);
}
//关闭流对象
fileInputStream.close();
return String.valueOf(stringBuffer);
}
/**
* @Author LiDong
* @Description //FileOutputStream 写文件
* @Date 21:56 2021/1/4
* @Param [file, res]
* @return void
**/
public static void writeByFileOutPutStream(File file,String res) throws IOException {
//放到字节数组里
byte[] words = res.getBytes();
FileOutputStream fileOutPutStream = new FileOutputStream(file,true);
//写入文件
fileOutPutStream.write(words,0,words.length);
System.out.println("文件已更新");
//关闭流
fileOutPutStream.close();
}
}
. . . To be continued!