Core function and usage: Core in Java Class Library :: IO Framework Guide

Core function and usage: Core in Java Class Library :: IO Framework Guide Core :: IO framework is an important class library provided by the Java standard library for processing input and output operations.It provides a set of powerful and flexible classes and interfaces for reading and writing data, management files and directory, and processing network communication.The core functions and usage of some Core :: IO framework will be introduced below. 1. File read and write operation: Core :: IO framework provides a variety of classes and interfaces for reading and writing files.You can use the File class to represent the abstract path name of the file or directory. Use the FileReader and FileWriter class to read and write character data, and use FileInputStream and FileoutPutstream class to read and write byte data.Below is a simple example code that demonstrates how to use FileReader to read the content of the file and print it: try (FileReader reader = new FileReader("file.txt")) { int c; while ((c = reader.read()) != -1) { System.out.print((char) c); } } catch (IOException e) { e.printStackTrace(); } 2. Cushion reading: In order to improve the reading and writing performance, the Core :: IO framework provides buffer streaming class, such as BufferedReader and BufferedWrit, which is used for buffer processing of character streams.The buffer flow can read multiple characters at one time or write multiple characters, reducing the number of times of access to the underlying data source, and improving the reading and writing efficiency.The following is a sample code that demonstrates how to use the bufferedReader to read the content of the file according to the line: try (BufferedReader reader = new BufferedReader(new FileReader("file.txt"))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } 3. File and directory management: Core :: IO framework provides some classes and interfaces related to files and directory management, such as File and PATH classes.You can use these classes to obtain the attribute information of the file and directory, create and delete files and directory, and traverse the files in the directory.The following is an example code that demonstrates how to use the file class to create new files and delete files: File file = new File("newfile.txt"); try { if (file.createNewFile()) { System.out.println ("Successful creation"); } } catch (IOException e) { e.printStackTrace(); } if (file.exists()) { if (file.delete()) { System.out.println ("File Delete Successful"); } } 4. Network communication: Core :: IO framework also provides some classes and interfaces for processing network communication.You can use the socket class and the Serversocket class to achieve communication between clients and servers.The socket class is used to create a client socket, which can be transmitted with the server through it.The Serversocket class is used to create a socket on the server side, waiting for the client to connect and receive data.The following is a simple example code that demonstrates how to use the socket class to establish a connection and send a request with the server: try (Socket socket = new Socket("localhost", 8080); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) { out.println("Hello, Server!"); String response = in.readLine(); System.out.println("Server response: " + response); } catch (IOException e) { e.printStackTrace(); } In summary, the Core :: IO framework is an important part of the Java class library, which provides rich and powerful functions for processing input and output operations, files and directory management, and network communication.By using these classes and interfaces reasonably, data can be processed and communicated more efficiently.