Java标准库中的IO操作,如读写文件,网络通信等案例分析

原创 超、凢脫俗 2024-12-04 22:09 100阅读 0赞

在Java标准库中,IO(Input/Output)操作主要通过以下几个类和接口来实现:

  1. File:代表一个文件或目录。进行读写等IO操作。

案例:

  1. import java.io.File;
  2. public class FileExample {
  3. public static void main(String[] args) {
  4. // 创建一个File对象,代表当前工作目录
  5. File currentDir = new File();
  6. // 检查当前目录是否存在
  7. if (currentDir.exists()) {
  8. System.out.println("Current directory exists: " + currentDir.getAbsolutePath());
  9. // 进行读写操作,这里以写入内容为例
  10. try {
  11. String content = "Hello, Java!";
  12. File fileToWrite = new File(currentDir, "example.txt"));
  13. Files.write(fileToWrite.toPath(), content.getBytes(StandardCharsets.UTF_8)));
  14. System.out.println("Content written successfully to: " + fileToWrite.getAbsolutePath());
  15. } catch (IOException e) {
  16. System.err.println("Error occurred while writing content: " + e.getMessage());
  17. e.printStackTrace();
  18. }
  19. } else {
  20. System.out.println("Current directory does not exist.");
  21. }
  22. }
  23. }
  1. InputStream/OutputStream:提供读写字节流的API。

案例:

  1. import java.io.*;
  2. public class IOExample {
  3. public static void main(String[] args) throws IOException {
  4. // 1. 创建一个文件对象,用于读写操作
  5. File file = new File("example.txt");
  6. // 2. 检查文件是否存在,不存在则创建
  7. if (file.exists()) {
  8. System.out.println("File already exists: " + file.getAbsolutePath());
  9. // 3. 如果要写入内容,则使用输出流
  10. try (OutputStream outputStream = new FileOutputStream(file)) {
  11. byte[] content = "Hello, Java!".getBytes(StandardCharsets.UTF_8));
  12. outputStream.write(content);
  13. System.out.println("Content written successfully to the file.");
  14. } catch (IOException e) {
  15. System.err.println("Error occurred while writing content: " + e.getMessage());
  16. e.printStackTrace();
  17. }
  18. } else {
  19. System.out.println("File does not exist, creating now...");
  20. // 创建新文件
  21. try (OutputStream outputStream = new FileOutputStream(file)) {
  22. byte[] content = "Hello, Java!".getBytes(StandardCharsets.UTF_8));
  23. outputStream.write(content);
  24. System.out.println("New file created successfully.");
  25. } catch (IOException e) {
  26. System.err.println("Error occurred while creating file: " + e.getMessage());
  27. e.printStackTrace();
  28. }
  29. }
  30. }
  31. }
  1. Network通信:使用java.net包中的类来实现网络通信,例如Socket、ServerSocket等。

案例:

  1. import java.io.*;
  2. import java.net.*;
  3. public class NetworkingExample {
  4. public static void main(String[] args) throws IOException, SocketException {
  5. // 创建Socket连接服务器
  6. String serverHost = "localhost"; // 本地服务器IP地址或域名
  7. int serverPort = 8080; // 服务器端口
  8. try (Socket socket = new Socket(serverHost, serverPort))) {
  9. System.out.println("Connected to server successfully...");
  10. // 创建输出流,用于发送数据
  11. OutputStream outputStream = socket.getOutputStream();
  12. // 发送字符串数据
  13. String messageToSend = "Hello, Server!";
  14. byte[] messageBytes = messageToSend.getBytes(StandardCharsets.UTF_8));
  15. outputStream.write(messageBytes);
  16. // 接收服务器响应数据
  17. InputStream inputStream = socket.getInputStream();
  18. BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)));
  19. String responseFromServer;
  20. while ((responseFromServer = reader.readLine())) != null) {
  21. System.out.println("Received server response: " + responseFromServer));
  22. }
  23. } catch (SocketException e) {
  24. System.err.println("Error occurred while establishing socket connection: " + e.getMessage());
  25. e.printStackTrace();
  26. } catch (IOException e) {
  27. System.err.println("Error occurred while sending/receiving data from server: " + e.getMessage());
  28. e.printStackTrace();
  29. }
  30. }
  31. }

以上案例展示了Java标准库中IO操作的几种常见方式,包括读写文件、网络通信等。

文章版权声明:注明蒲公英云原创文章,转载或复制请以超链接形式并注明出处。

发表评论

表情:
评论列表 (有 0 条评论,100人围观)

还没有评论,来说两句吧...

相关阅读