使用java将统计结果按照手机归属地不同省份输出到不同文件
时间: 2024-03-16 22:45:24 浏览: 24
可以通过以下步骤实现:
1. 读取存储手机归属地的文件,将省份信息存储到一个列表中。
2. 读取存储统计结果的文件,将每个手机号码的归属地信息和统计结果存储到一个 Map 中。
3. 遍历 Map,将手机号码归属地对应的统计结果累加到省份对应的统计结果中。
4. 遍历省份列表,将每个省份的统计结果输出到对应的文件中。
下面是示例代码:
```java
import java.io.*;
import java.util.*;
public class PhoneStatistic {
public static void main(String[] args) throws IOException {
// 读取手机号码归属地信息
Map<String, String> phoneLocationMap = readPhoneLocationFile("phone_location.txt");
// 读取统计结果
Map<String, Integer> statisticMap = readStatisticFile("statistic.txt");
// 统计每个省份的结果
Map<String, Integer> provinceStatisticMap = new HashMap<>();
for (Map.Entry<String, Integer> entry : statisticMap.entrySet()) {
String phone = entry.getKey();
String location = phoneLocationMap.get(phone);
if (location != null) {
String province = location.split(" ")[0];
provinceStatisticMap.put(province, provinceStatisticMap.getOrDefault(province, 0) + entry.getValue());
}
}
// 输出结果到不同文件
for (String province : provinceStatisticMap.keySet()) {
String fileName = province + ".txt";
int count = provinceStatisticMap.get(province);
try (PrintWriter writer = new PrintWriter(new FileWriter(fileName))) {
writer.println(count);
}
}
}
private static Map<String, String> readPhoneLocationFile(String fileName) throws IOException {
Map<String, String> phoneLocationMap = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
phoneLocationMap.put(parts[0], parts[1]);
}
}
return phoneLocationMap;
}
private static Map<String, Integer> readStatisticFile(String fileName) throws IOException {
Map<String, Integer> statisticMap = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
String phone = parts[0];
int count = Integer.parseInt(parts[1]);
statisticMap.put(phone, count);
}
}
return statisticMap;
}
}
```
其中,`phone_location.txt` 文件存储手机号码归属地信息,格式为 `phone,location`。`statistic.txt` 文件存储统计结果,格式为 `phone,count`。程序将统计结果按照手机归属地不同省份输出到不同文件中,文件名为省份名加 `.txt` 后缀。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)