package com.company;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
String fileName = "/Users/dllo/Desktop/area.txt";
String content = readFileByLines(fileName);
System.out.println(content);
String str[] = content.split("\n");
System.out.println("666" + Arrays.toString(str));
ArrayList<HashMap> proList = new ArrayList<HashMap>();
for (int i = 0; i < str.length; i++) {
//省
if (!str[i].startsWith(" ")) {
HashMap<Object, Object> proHashMap = new HashMap<>();
proHashMap.put("proName", str[i]);
ArrayList<HashMap> cityList = new ArrayList<HashMap>();
proHashMap.put("cityList", cityList);
proList.add(proHashMap);
}
//市
if (str[i].startsWith(" ") && !str[i].startsWith(" ")) {
HashMap<Object, Object> cityHashMap = new HashMap<>();
cityHashMap.put("cityName", str[i]);
ArrayList<String> areaList = new ArrayList<String>();
cityHashMap.put("areaList", areaList);
HashMap<Object, Object> proHashMap = new HashMap<>();
proHashMap = (HashMap) proList.get(proList.size() - 1);
ArrayList<HashMap> cityList = new ArrayList<>();
cityList = (ArrayList) proHashMap.get("cityList");
cityList.add(cityHashMap);
}
//区
if (str[i].startsWith(" ")) {
HashMap<Object, Object> proHashMap = new HashMap<>();
proHashMap = (HashMap) proList.get(proList.size() - 1);
ArrayList<HashMap<Object, Object>> cityList = new ArrayList<>();
cityList = (ArrayList) proHashMap.get("cityList");
HashMap<Object, Object> cityHashMap = new HashMap<>();
cityHashMap = (HashMap) cityList.get(cityList.size() - 1);
ArrayList<String> areaList = new ArrayList<>();
areaList = (ArrayList) cityHashMap.get("areaList");
areaList.add(str[i]);
}
}
System.out.println("**~~*~~~~**" + proList);
}
public static String readFileByLines(String fileName) {
String content = null;
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
content = content + tempString + "\n";
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return content;
}
}

java + 省市区的数据结构划分
最新推荐文章于 2025-03-10 11:04:49 发布