java实现停车场管理系统的数据导入导出功能利用BufferedReader和BufferedWriter
时间: 2023-12-12 20:33:53 浏览: 171
以下是Java实现停车场管理系统数据导入导出功能利用BufferedReader和BufferedWriter的示例代码:
1. 数据导出功能
```java
import java.io.*;
public class ExportData {
public static void main(String[] args) throws IOException {
// 读取停车场数据
String parkingData = readParkingData();
// 将停车场数据写入文件
File file = new File("parking_data.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(parkingData);
writer.close();
System.out.println("数据导出成功!");
}
// 读取停车场数据
private static String readParkingData() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("parking_data.txt"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
}
```
2. 数据导入功能
```java
import java.io.*;
public class ImportData {
public static void main(String[] args) throws IOException {
// 读取导入的数据
File file = new File("parking_data.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
String importData = sb.toString();
// 将导入的数据写入停车场
writeParkingData(importData);
System.out.println("数据导入成功!");
}
// 写入停车场数据
private static void writeParkingData(String data) throws IOException {
File file = new File("parking_data.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(data);
writer.close();
}
}
```
阅读全文