From entry to proficiency: gradually learn Core in the Java class library :: IO framework

From entry to proficiency: gradually learn Core in the Java class library :: IO framework Java's Core :: IO framework is an important part of processing input and output.It provides developers with a class and interface of various processing documents, streaming and network connections.In this article, we will gradually learn the core :: IO framework in the Java class library, including how to use different classes and interfaces to read and write files, and how to handle network connection. 1. File read and write 1. File class and path: Java provides the File class to process files and directory operations.You can use the File class to create, delete, rename files and directory, and obtain the attributes of the file and directory. // Create a file object File file = new File("example.txt"); // Check whether the file exists if (file.exists()) { // Get file name String fileName = file.getName(); System.out.println ("File name:" + FILENAME); // Get the file path String filePath = file.getAbsolutePath(); System.out.println ("File path:" + filepath); // Get the file size long fileSize = file.length(); System.out.println ("File size:" + Filesize + "byte"); } 2. Input output stream: The input and output flow in Java is used to process data reading and writing.Use the InputStream class and the OutputStream class to read and write binary data.The use of the Reader and Writer classes can be read and written into character data. // Read the content of the file by flowing try (InputStream inputStream = new FileInputStream("example.txt")) { int data; while ((data = inputStream.read()) != -1) { System.out.print((char) data); } } catch (IOException e) { e.printStackTrace(); } // Write the content of the file through flowing try (OutputStream outputStream = new FileOutputStream("example.txt")) { outputStream.write("Hello, World!".getBytes()); } catch (IOException e) { e.printStackTrace(); } 3. Cushion flow: In order to improve reading and writing performance, Java provides buffer flow.The buffEredInputStream and BufferedOutPutstream class are used to provide cushioning function, reduce the number of interaction with the disk, and increase the read and write speed. // Use the content of the file to read the file content try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } // Use the buffer stream to write the file content try (BufferedWriter writer = new BufferedWriter(new FileWriter("example.txt"))) { writer.write("Hello, World!"); } catch (IOException e) { e.printStackTrace(); } 2. Network connection processing 1. URL class: Java's URL class is used to process URL connections.You can use the URL class to open the connection, read data, and get connection information. try { URL url = new URL("https://www.example.com"); // Open the connection HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // Get the response code int responseCode = connection.getResponseCode(); System.out.println ("response code:" + responsecode); // Read the data try (InputStream inputStream = connection.getInputStream()) { int data; while ((data = inputStream.read()) != -1) { System.out.print((char) data); } } } catch (IOException e) { e.printStackTrace(); } 2. Socket class: Java's socket class is used to handle network socket connection.You can use the socket class to connect the server and send/receive data. try (Socket socket = new Socket("localhost", 8080)) { // send data OutputStream outputStream = socket.getOutputStream(); outputStream.write("Hello, Server!".getBytes()); // Receive data InputStream inputStream = socket.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String response = reader.readLine(); System.out.println ("Server response:" + response); } catch (IOException e) { e.printStackTrace(); } 3. Summary Through the introduction of this article, we learned the Core :: IO framework in the Java class library.We understand how to use File class processing files and directory operations, how to use the input and output stream for file read and write, and how to process network connection.These knowledge will help you better handle the input and output tasks in the Java program.Hope this article will be helpful to your learning!