I amMr. Aojiaolu, a person who accumulates, learns, shares and grows.
If you think the content of the article is acceptable, I hope you will not hesitate to "One-click triple connection". I hope there are deficiencies in the article. Please add your doubts, insights and weird questions you encountered during the interview in the comment area
Table of contents
1. Knowledge system of Java IO framework
2. Java IO classification (transmission, operation)
1. IO understanding classification-from the perspective of transmission method
2. IO understanding classification - from the perspective of data operations
3. Java IO-Usage of common classes
3. Realize line-by-line output of the contents of a text file
4. Serialization & Serializable & transient
1. Knowledge system of Java IO framework
Creating a good I/O system is not an easy task, and the difficulty seems to come from the need to cover all possibilities. Because there are not only various I/O sources (files, consoles, network connections, etc.), but also need to communicate with these I/O sources in many different ways (sequential, random, buffered, binary, character-by-character, line-by-line , by word) to communicate. The designers of Java class libraries solved this problem by creating a large number of classes.
The most complete source code for detailed interpretation of io flow: Java8 I/O source code-catalog_Pan Weiwei's blog-CSDN blog
2. Java IO classification (transmission, operation)
1. IO understanding classification-from the perspective of transmission method
From the perspective of data transmission method or transportation method, the IO class can be divided into: byte stream and character stream
Byte stream (the overall structure is as follows, some derived classes are missing)
Character stream (the overall structure is as follows, some derived classes are missing)
The difference between byte stream and character stream
-
The byte stream reads a single byte, and the character stream reads a single character (one character corresponds to different bytes depending on the encoding. For example, UTF-8 encoded Chinese characters are 3 bytes, and GBK encoded Chinese characters are 2 bytes. byte.)
-
The byte stream is used to process binary files (pictures, MP3, video files), and the character stream is used to process text files (which can be regarded as special binary files that use a certain encoding and can be read by humans).
Byte to character Input/OutputStreamReader/Writer
-
In GBK encoding, Chinese characters occupy 2 bytes and English characters occupy 1 byte;
-
In UTF-8 encoding, Chinese characters occupy 3 bytes and English characters occupy 1 byte;
-
In UTF-16be encoding, Chinese characters and English characters both occupy 2 bytes.
2. IO understanding classification - from the perspective of data operations
From the perspective of data sources or operating objects, IO classes can be divided into:
文件(file):FileInputStream、FileOutputStream、FileReader、FileWriter
Array([]):
-
Byte array (byte[]): ByteArrayInputStream, ByteArrayOutputStream
-
Character array (char[]): CharArrayReader, CharArrayWriter
Pipe operations: PipedInputStream, PipedOutputStream, PipedReader, PipedWriter
Basic data types: DataInputStream, DataOutputStream
Buffering operations: BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter
Print: PrintStream, PrintWriter
Object serialization and deserialization: ObjectInputStream, ObjectOutputStream
Conversion: InputStreamReader, OutputStreamWriter
3. Java IO-Usage of common classes
-
Disk operations: File
-
Byte operations: InputStream and OutputStream
-
Character operations: Reader and Writer
-
Object operations: Serializable
-
Network operations: Socket
1. File related
The File class can be used to represent file and directory information, but it does not represent the contents of the file. List all files in a directory recursively:
public static void listAllFiles(File dir) {
if (dir == null || !dir.exists()) {
return;
}
if (dir.isFile()) {
System.out.println(dir.getName());
return;
}
for (File file : dir.listFiles()) {
listAllFiles(file);
}
}
2. Byte stream related
public static void copyFile(String src, String dist) throws IOException {
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dist);
byte[] buffer = new byte[20 * 1024];
// read() 最多读取 buffer.length 个字节
// 返回的是实际读取的个数
// 返回 -1 的时候表示读到 eof,即文件尾
while (in.read(buffer, 0, buffer.length) != -1) {
out.write(buffer);
}
in.close();
out.close();
}
3. Realize line-by-line output of the contents of a text file
public static void readFileContent(String filePath) throws IOException {
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
// 装饰者模式使得 BufferedReader 组合了一个 Reader 对象
// 在调用 BufferedReader 的 close() 方法时会去调用 Reader 的 close() 方法
// 因此只要一个 close() 调用即可
bufferedReader.close();
}
4、序列化 & Serializable & transient
-
Serialization: ObjectOutputStream.writeObject()
-
Deserialization: ObjectInputStream.readObject()
public static void main(String[] args) throws IOException, ClassNotFoundException {
A a1 = new A(123, "abc");
String objectFile = "file/a1";
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(objectFile));
objectOutputStream.writeObject(a1);
objectOutputStream.close();
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(objectFile));
A a2 = (A) objectInputStream.readObject();
objectInputStream.close();
System.out.println(a2);
}
private static class A implements Serializable {
private int x;
private String y;
A(int x, String y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "x = " + x + " " + "y = " + y;
}
}
private transient Object[] elementData;
5. Network support in Java:
-
InetAddress: used to represent hardware resources on the network, that is, IP addresses;
-
URL: Uniform Resource Locator;
-
Sockets: Use TCP protocol to implement network communication;
-
Datagram: Implement network communication using UDP protocol.
InetAddress
InetAddress.getByName(String host);
InetAddress.getByAddress(byte[] address);
6、URL
public static void main(String[] args) throws IOException {
URL url = new URL("http://www.baidu.com");
/* 字节流 */
InputStream is = url.openStream();
/* 字符流 */
InputStreamReader isr = new InputStreamReader(is, "utf-8");
/* 提供缓存功能 */
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
7、Sockets
-
ServerSocket: Server-side class
-
Socket: client class
-
The server and client perform input and output through InputStream and OutputStream.
8、Datagram
-
DatagramSocket: Communication class
-
DatagramPacket: Data packet class
The series of articles continues to be updated. Search "Mr. Aojiaolu " on WeChat and reply [ Interview】Prepared interview materials for first-tier manufacturers.