import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class j1 {
static void main() throws IOException {
String familyNameNet =" https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0 ";
String boyNameNet =" http://www.haoming8.cn/baobao/10881.html ";
String girlNameNet ="http://www.haoming8.cn/baobao/7641.html ";
String familyNameStr = weCrawler(familyNameNet);
String boyNameStr = weCrawler(boyNameNet);
String girlNameStr = weCrawler(girlNameNet);
ArrayList<String> flList = getData(familyNameStr,"[\\u4E00-\\u9FA5]", 0);
ArrayList<String> blList = getData(boyNameStr,"([\\u4E00-\\u9FA5]{2})(?=、|。)", 1);
ArrayList<String> glList = getData(girlNameStr,"([\\u4E00-\\u9FA5]{2})(?= |、|。)", 1);
ArrayList<String> fList = new ArrayList<>();
for(String str : flList) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
fList.add(c + "");
}
}
ArrayList<String> bList = new ArrayList<>();
for(String str : blList) {
if(!bList.contains(str)){
bList.add(str);
}
}
ArrayList<String> gList = new ArrayList<>();
for(String str : glList) {
String[] arr = str.split(" ");
for(int i = 0; i < arr.length; i++) {
gList.add(arr[i]);
}
}
getInfos( fList, bList, gList, 50, 50);
}
public static ArrayList<String> getInfos(ArrayList<String> fList,ArrayList<String> bList, ArrayList<String> gList, int bn, int gn) {
HashSet<String> bhs = new HashSet<>();
while(true){
if(bhs.size() == bn) {
break;
}
Collections.shuffle(fList);
Collections.shuffle(bList);
bhs.add(fList.get(0) + bList.get((0)));
}
HashSet<String> ghs = new HashSet<>();
while(true){
if(bhs.size() == bn) {
break;
}
Collections.shuffle(fList);
Collections.shuffle(gList);
ghs.add(fList.get(0) + gList.get((0)));
}
System.out.println(bhs);
System.out.println(ghs);
return null;
}
public static ArrayList<String> getData(String str, String regex, int index) {
ArrayList<String> list = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
list.add(matcher.group(index));
}
return list;
}
//爬取
public static String weCrawler(String net) throws IOException {
StringBuilder sb = new StringBuilder();
URL url = new URL(net);
URLConnection conn = url.openConnection();
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
int ch;
while((ch = isr.read()) != -1) {
sb.append((char)ch);
}
isr.close();
return sb.toString();
}
}
随机生成假名字
一、Properties 配置文件核心基础
1. 核心定位与文件特性
- 文件定义 :以
.properties为后缀的纯文本配置文件,是 Java 中最基础、最常用的配置格式,专门用于存储键值对(Key-Value) 形式的配置参数。 - 核心优势 :
- 持久化存储:可将软件的设置、参数永久保存到本地,程序重启后仍能读取。
- 解耦代码:修改配置参数无需改动源代码、无需重新编译,直接编辑配置文件即可生效,极大提升开发与维护效率。
- 继承体系 :Properties 配置文件的操作核心类
java.util.Properties,是Hashtable的子类,属于 Map 双列集合体系,因此拥有 Map 集合的所有特性(键唯一、键值映射)。
2. 配置文件语法规范(全量细节)
- 核心格式 :
键 = 值或键: 值(推荐使用等号,可读性更强),键和值均为字符串类型,无需加引号 。- 示例:
width = 603、image = animal3、wechat = about.png
- 示例:
- 注释规则 :以
#或!开头的行为注释,程序读取时会自动忽略,注释需单独占一行。- 示例:
# 图片尺寸配置、! 微信二维码路径
- 示例:
- 空格处理 :键和值首尾的空格会被自动忽略 ,若值中需包含空格,直接书写即可(如
title = 欢迎使用)。 - 换行处理 :若值内容过长,可使用反斜杠
\实现换行,续行后的内容与原值为一个整体。- 示例:
content = 这是一段很长的配置信息,\n需要换行展示
- 示例:
- 编码规范 :传统 Properties 文件默认采用
ISO-8859-1编码,无法直接存储中文;JDK 9 及以上支持UTF-8编码,低版本需通过转义(\uXXXX)或指定编码读取解决中文乱码。
二、Properties 类核心方法(双列集合方法 + 特有读写方法)
Properties 类是操作 Properties 配置文件的专属工具,方法分为 "双列集合通用方法" 和 "配置文件特有读写方法" 两类,全量核心方法如下:
1. 作为双列集合的通用方法(继承自 Map/Hashtable)
表格
| 方法名 | 完整功能说明 |
|---|---|
Object put(Object key, Object value) |
向 Properties 集合中添加键值对(键和值本质为字符串,底层会自动转换) |
Object get(Object key) |
根据键获取对应值,返回值为 Object 类型,需手动强转为 String |
Set<Object> keySet() |
获取所有键的集合,用于遍历配置信息 |
Set<Map.Entry<Object, Object>> entrySet() |
获取所有键值对的集合,用于遍历配置信息 |
void clear() |
清空 Properties 集合中的所有键值对 |
boolean containsKey(Object key) |
判断集合中是否包含指定键 |
2. Properties 特有操作方法(简化字符串处理)
为避免频繁类型转换,Properties 提供了专门针对字符串的键值对操作方法,替代通用的 put 和 get:
表格
| 方法名 | 完整功能说明 |
|---|---|
String getProperty(String key) |
根据键获取对应值,直接返回 String 类型,最常用 |
String getProperty(String key, String defaultValue) |
根据键获取值,若键不存在,返回指定的默认值(避免空指针) |
Object setProperty(String key, String value) |
向集合中添加键值对,底层调用 put 方法,返回值为该键之前的旧值(无则为 null) |
Set<String> stringPropertyNames() |
获取所有键的集合,返回值为 Set<String>,比 keySet() 更便捷,专门用于遍历配置 |
3. 配置文件特有读写方法(核心核心)
实现 "集合 ↔ 配置文件" 的双向数据传输,是 Properties 类的核心价值,全量方法如下:
(1)读取配置文件到集合
表格
| 方法名 | 完整功能说明 | 异常处理 |
|---|---|---|
void load(InputStream inStream) |
从字节输入流中读取 Properties 配置文件,加载到集合中 | 抛出 IOException,需显式捕获 / 抛出 |
void load(Reader reader) |
从字符输入流中读取配置文件,支持指定编码(解决中文乱码) | 抛出 IOException,需显式捕获 / 抛出 |
void load(InputStream inStream, String comments) |
JDK 9 新增,读取字节流配置文件,同时指定注释编码 | 抛出 IOException,需显式捕获 / 抛出 |
(2)将集合写入配置文件
表格
| 方法名 | 完整功能说明 | 异常处理 |
|---|---|---|
void store(OutputStream out, String comments) |
将集合中的键值对,通过字节输出流 写入配置文件,comments 为配置文件注释(可为 null) |
抛出 IOException,需显式捕获 / 抛出 |
void store(Writer writer, String comments) |
将集合中的键值对,通过字符输出流写入配置文件,支持指定编码 | 抛出 IOException,需显式捕获 / 抛出 |
void storeToXML(OutputStream os, String comment) |
将集合数据写入 XML 格式的配置文件,拓展配置格式 | 抛出 IOException,需显式捕获 / 抛出 |
三、核心实操流程(读取 + 写入,全量步骤)
1. 读取 Properties 配置文件到集合
java
运行
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class PropertiesReadDemo {
public static void main(String[] args) {
// 1. 创建 Properties 集合对象
Properties prop = new Properties();
// 2. 创建字符输入流,关联配置文件(指定 UTF-8 编码解决中文乱码)
try (FileReader fr = new FileReader("config.properties", java.nio.charset.StandardCharsets.UTF_8)) {
// 3. 调用 load 方法,读取配置文件到集合
prop.load(fr);
// 4. 遍历集合(两种常用方式)
// 方式1:根据指定键获取值
String width = prop.getProperty("width");
System.out.println("图片宽度:" + width);
// 方式2:遍历所有键值对
Set<String> keys = prop.stringPropertyNames();
for (String key : keys) {
String value = prop.getProperty(key);
System.out.println(key + " = " + value);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. 将集合数据写入 Properties 配置文件
java
运行
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class PropertiesWriteDemo {
public static void main(String[] args) {
// 1. 创建 Properties 集合对象,添加配置信息
Properties prop = new Properties();
prop.setProperty("width", "800");
prop.setProperty("height", "600");
prop.setProperty("image", "animal4");
prop.setProperty("title", "配置文件示例"); // 包含中文
// 2. 创建字符输出流,关联目标配置文件(指定 UTF-8 编码)
try (FileWriter fw = new FileWriter("new_config.properties", java.nio.charset.StandardCharsets.UTF_8)) {
// 3. 调用 store 方法,将集合写入配置文件,注释为"图片配置信息"
prop.store(fw, "图片配置信息");
System.out.println("配置文件写入成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}