Java标准库中的IO操作,如读写文件,网络通信等案例分析
在Java标准库中,IO(Input/Output)操作主要通过以下几个类和接口来实现:
- File:代表一个文件或目录。进行读写等IO操作。
案例:
import java.io.File;
public class FileExample {
public static void main(String[] args) {
// 创建一个File对象,代表当前工作目录
File currentDir = new File();
// 检查当前目录是否存在
if (currentDir.exists()) {
System.out.println("Current directory exists: " + currentDir.getAbsolutePath());
// 进行读写操作,这里以写入内容为例
try {
String content = "Hello, Java!";
File fileToWrite = new File(currentDir, "example.txt"));
Files.write(fileToWrite.toPath(), content.getBytes(StandardCharsets.UTF_8)));
System.out.println("Content written successfully to: " + fileToWrite.getAbsolutePath());
} catch (IOException e) {
System.err.println("Error occurred while writing content: " + e.getMessage());
e.printStackTrace();
}
} else {
System.out.println("Current directory does not exist.");
}
}
}
- InputStream/OutputStream:提供读写字节流的API。
案例:
import java.io.*;
public class IOExample {
public static void main(String[] args) throws IOException {
// 1. 创建一个文件对象,用于读写操作
File file = new File("example.txt");
// 2. 检查文件是否存在,不存在则创建
if (file.exists()) {
System.out.println("File already exists: " + file.getAbsolutePath());
// 3. 如果要写入内容,则使用输出流
try (OutputStream outputStream = new FileOutputStream(file)) {
byte[] content = "Hello, Java!".getBytes(StandardCharsets.UTF_8));
outputStream.write(content);
System.out.println("Content written successfully to the file.");
} catch (IOException e) {
System.err.println("Error occurred while writing content: " + e.getMessage());
e.printStackTrace();
}
} else {
System.out.println("File does not exist, creating now...");
// 创建新文件
try (OutputStream outputStream = new FileOutputStream(file)) {
byte[] content = "Hello, Java!".getBytes(StandardCharsets.UTF_8));
outputStream.write(content);
System.out.println("New file created successfully.");
} catch (IOException e) {
System.err.println("Error occurred while creating file: " + e.getMessage());
e.printStackTrace();
}
}
}
}
- Network通信:使用java.net包中的类来实现网络通信,例如Socket、ServerSocket等。
案例:
import java.io.*;
import java.net.*;
public class NetworkingExample {
public static void main(String[] args) throws IOException, SocketException {
// 创建Socket连接服务器
String serverHost = "localhost"; // 本地服务器IP地址或域名
int serverPort = 8080; // 服务器端口
try (Socket socket = new Socket(serverHost, serverPort))) {
System.out.println("Connected to server successfully...");
// 创建输出流,用于发送数据
OutputStream outputStream = socket.getOutputStream();
// 发送字符串数据
String messageToSend = "Hello, Server!";
byte[] messageBytes = messageToSend.getBytes(StandardCharsets.UTF_8));
outputStream.write(messageBytes);
// 接收服务器响应数据
InputStream inputStream = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)));
String responseFromServer;
while ((responseFromServer = reader.readLine())) != null) {
System.out.println("Received server response: " + responseFromServer));
}
} catch (SocketException e) {
System.err.println("Error occurred while establishing socket connection: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Error occurred while sending/receiving data from server: " + e.getMessage());
e.printStackTrace();
}
}
}
以上案例展示了Java标准库中IO操作的几种常见方式,包括读写文件、网络通信等。
还没有评论,来说两句吧...