举例 Java IO的使用场景及代码
时间: 2023-09-26 19:07:07 浏览: 112
### 回答1:
Java IO的使用场景可以涵盖从文件读写到网络数据流的操作,比如说从本地文件中读取数据到内存,或者从网络中读取文件到本地。以下是一个简单的示例代码,演示了如何使用Java IO从本地文件中读取数据:import java.io.FileReader;
import java.io.BufferedReader;public class ReadFileExample {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("example.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
### 回答2:
Java IO(输入/输出)是Java编程语言中用于处理输入和输出的机制。它提供了一组类和接口,用于读取和写入不同类型的数据。
以下是几个Java IO的使用场景及相应代码示例:
1. 读取和写入文本文件:
```java
import java.io.*;
public class FileReadWrite {
public static void main(String[] args) {
// 读取文件
try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// 写入文件
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
writer.write("Hello, World!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
2. 读取和写入二进制文件:
```java
import java.io.*;
public class BinaryFileReadWrite {
public static void main(String[] args) {
// 读取文件
try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("input.bin"))) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
System.out.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
// 写入文件
try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("output.bin"))) {
byte[] data = {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x2C, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21};
outputStream.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
3. 读取和写入网络连接:
```java
import java.io.*;
import java.net.*;
public class NetworkReadWrite {
public static void main(String[] args) {
// 读取网页内容
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new URL("https://www.example.com").openStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// 写入HTTP请求
try {
URL url = new URL("http://www.example.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()))) {
writer.write("data=example");
}
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这些示例演示了Java IO在不同场景下的用法,包括文件读写和网络连接。实际应用中,Java IO还可以用于处理键盘输入、数据库访问等其他场景。
### 回答3:
Java IO(输入/输出)是Java编程语言中用于处理输入和输出操作的API。它提供了丰富的类和方法,可以用于读取和写入文件、网络操作、序列化和反序列化对象等。以下是一些Java IO的使用场景和相应的代码示例:
1. 读取文件内容并打印每一行:
```java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileReadingExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("filename.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
2. 写入文件内容:
```java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class FileWritingExample {
public static void main(String[] args) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("filename.txt"))) {
bw.write("Hello, World!");
bw.newLine();
bw.write("This is a line.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
3. 读取网络资源:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class URLReadingExample {
public static void main(String[] args) {
try {
URL url = new URL("http://example.com");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
4. 序列化和反序列化对象:
```java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class SerializationExample {
public static void main(String[] args) {
try {
MyObject obj = new MyObject("data");
// 序列化对象到文件
FileOutputStream fos = new FileOutputStream("object.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
fos.close();
// 从文件反序列化对象
FileInputStream fis = new FileInputStream("object.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
MyObject deserializedObj = (MyObject) ois.readObject();
fis.close();
System.out.println(deserializedObj.getData());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
static class MyObject implements Serializable {
private String data;
public MyObject(String data) {
this.data = data;
}
public String getData() {
return data;
}
}
}
```
以上是几个Java IO的使用场景及示例代码,展示了如何读取和写入文件、读取网络资源以及序列化和反序列化对象。Java IO是Java编程中常用的功能之一,能处理各种输入和输出操作。
阅读全文