IO流综合练习
文章目录
- IO流综合练习
- 制造假数据
- 需求
- 利用糊涂包制造假数据,并写入文件中
- 随机点名器
- Student标准JavaBean类
- 实现代码
- names.txt文件中的内容
- 登录
制造假数据
需求
制造假数据也是开发中的一个能力,在各个网上爬取数据,是其中一个方法
package com.example.demo.myiotest1;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test1 {
public static void main(String[] args) throws IOException {
/* 制造假数据:
获取姓氏:https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0
获取男生名字:http://www.haoming8.cn/baobao/10881.html
获取女生名字:http://www.haoming8.cn/baobao/7641.html
*/
// 1.定义变量记录网址
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";
// 2.爬取数据,把网址上所有的数据拼接成一个字符串
String familyNameStr = webCrawler(familyNameNet);
String boyNameStr = webCrawler(boyNameNet);
String girlNameStr = webCrawler(girlNameNet);
// 3.通过正则表达式,把其中符合要求的数据获取出来
ArrayList<String> familyNameTempList = getDate(familyNameStr,"([\\u4E00-\\u9FA5]{4})(,|。)",1);
ArrayList<String> boyNameTempList = getDate(boyNameStr,"([\\u4E00-\\u9FA5]{2})(、|。)",1);
ArrayList<String> girlNameTempList = getDate(girlNameStr,"(.. ){4}..",0);
// 4.处理数据
// familyNameTempList(姓氏)
// 处理方案:把每一个姓氏拆开并添加到一个新的集合当中
ArrayList<String> familyNameList = new ArrayList<>();
for (String str : familyNameTempList) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
familyNameList.add(c+"");
}
}
//boyNameTempList
//处理方案:去除其中的重复数据
ArrayList<String> boyList = new ArrayList<>();
for (String str : boyNameTempList) {
if(!boyList.contains(str)){
boyList.add(str);
}
}
//girlNameTempList
//处理方案:把里面的每一个元素用空格进行切割,得到每一个女生的名字
ArrayList<String> girlList = new ArrayList<>();
for (String str : girlNameTempList) {
String[] arr = str.split(" ");
for (int i = 0; i < arr.length; i++) {
girlList.add(arr[i]);
}
}
// 5.生成数据
ArrayList<String> list = getInfos(familyNameList,boyList,girlList,70,80);
Collections.shuffle(list);
System.out.println(list);
// 6.写出数据
BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\name.txt"));
for (String str : list) {
bw.write(str);
bw.newLine();
}
bw.close();
}
/*
* 作用:
* 获取男生和女生的信息:姓名(唯一)-性别-年龄
* 形参:
* 参数一:装着姓氏的集合
* 参数二:装着男生名字的集合
* 参数三:装着女生名字的集合
* 参数四:男生的个数
* 参数五:女生的个数
* */
public static ArrayList<String> getInfos(ArrayList<String> familyNameList,ArrayList<String> boyList,ArrayList<String> girlList,int boyCount,int girlCount){
// 1.生成男生不重复的名字
HashSet<String> boyhs = new HashSet<>();
while (true){
if(boyhs.size() == boyCount){
break;
}
//随机
Collections.shuffle(familyNameList);
Collections.shuffle(boyList);
boyhs.add(familyNameList.get(0)+boyList.get(0));
}
// 2.生成女生不重复的名字
HashSet<String> girlhs = new HashSet<>();
while (true){
if(girlhs.size() == girlCount){
break;
}
//随机
Collections.shuffle(familyNameList);
Collections.shuffle(girlList);
girlhs.add(familyNameList.get(0)+girlList.get(0));
}
ArrayList<String> list = new ArrayList<>();
Random r = new Random();
for (String boyName:boyhs){
int age = r.nextInt(10)+18;
list.add(boyName+"-男-"+ age);
}
// 4.生成女生的信息并添加到集合当中
for (String girlName:girlhs){
int age = r.nextInt(8)+18;
list.add(girlName+"-女-"+ age);
}
return list;
}
/*
* 作用:根据正则表达式获取字符串中的数据
*参数一:
* 完整的字符串
* 参数二:
* 正则表达式
* 参数三:
*
* 返回值:
* 真正想要的数据
*
* */
private static ArrayList getDate(String str, String regex,int index) {
// 1.创建集合存放数据
ArrayList<String> list = new ArrayList<>();
// 2.按照正则表达式的规则,去获取数据
Pattern pattern = Pattern.compile(regex);
// 按照Pattern的规则,到str当中获取数据
Matcher matcher = pattern.matcher(str);
while (matcher.find()){
list.add(matcher.group(index));
}
return list;
}
/*
* 作用:
* 从网络中爬取数据,把数据拼接成字符串返回
* 形参:
* 网址
* 返回值:
* 爬取到的所有数据
* */
public static String webCrawler(String net) throws IOException {
// 1.定义StringBuilder拼接爬取到的数据
StringBuilder sb = new StringBuilder();
// 2.创建一个URL对象
URL url = new URL(net);
// 3.链接上这个网址
// 细节:保证网络是畅通的,而且这个网址是可以链接上的
URLConnection conn = url.openConnection();
// 4.读取数据
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
int ch;
while ((ch = isr.read()) != -1){
sb.append((char) ch);
}
// 5.释放资源
isr.close();
// 6.把读取到的数据返回
return sb.toString();
}
}
利用糊涂包制造假数据,并写入文件中
//Maven项目中
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.29</version>
</dependency>
//Gradle
implementation'cn.hutool:hutool-all:5.8.29'
package com.example.demo.myiotest1;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;
import java.util.*;
public class Test2 {
public static void main(String[] args) {
//利用糊涂包生成假数据,并写到文件中
//1.定义网址
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";
//2.爬取数据
String familyNameStr = HttpUtil.get(familyNameNet);
String boyNameStr = HttpUtil.get(boyNameNet);
String girlNameStr = HttpUtil.get(girlNameNet);
//3.利用正则表达式获取数据
List<String> familyNameTempList = ReUtil.findAll("([\\u4E00-\\u9FA5]{4})(,|。)", familyNameStr, 1);
List<String> boyNameTempList = ReUtil.findAll("([\\u4E00-\\u9FA5]{2})(、|。)", boyNameStr, 1);
List<String> girlNameTempList = ReUtil.findAll("(.. ){4}..", girlNameStr, 0);
//4.处理数据
ArrayList<String> familyNameList = new ArrayList<>();
for (String str : familyNameTempList) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
familyNameList.add(c+"");
}
}
//boyNameTempList
//处理方案:去除其中的重复数据
ArrayList<String> boyList = new ArrayList<>();
for (String str : boyNameTempList) {
if(!boyList.contains(str)){
boyList.add(str);
}
}
//girlNameTempList
//处理方案:把里面的每一个元素用空格进行切割,得到每一个女生的名字
ArrayList<String> girlList = new ArrayList<>();
for (String str : girlNameTempList) {
String[] arr = str.split(" ");
for (int i = 0; i < arr.length; i++) {
girlList.add(arr[i]);
}
}
//5.生成数据
ArrayList<String> list = getInfos(familyNameList,boyList,girlList,70,80);
Collections.shuffle(list);
System.out.println(list);
//6.写出数据
//细节:
//糊涂包的相对路径,不是相对于当前项目而言的,而是相对于class文件而言的
FileUtil.writeLines(list,"names.txt","UTF-8");
}
/*
* 作用:
* 获取男生和女生的信息:姓名(唯一)-性别-年龄
* 形参:
* 参数一:装着姓氏的集合
* 参数二:装着男生名字的集合
* 参数三:装着女生名字的集合
* 参数四:男生的个数
* 参数五:女生的个数
* */
public static ArrayList<String> getInfos(ArrayList<String> familyNameList,ArrayList<String> boyList,ArrayList<String> girlList,int boyCount,int girlCount){
// 1.生成男生不重复的名字
HashSet<String> boyhs = new HashSet<>();
while (true){
if(boyhs.size() == boyCount){
break;
}
//随机
Collections.shuffle(familyNameList);
Collections.shuffle(boyList);
boyhs.add(familyNameList.get(0)+boyList.get(0));
}
// 2.生成女生不重复的名字
HashSet<String> girlhs = new HashSet<>();
while (true){
if(girlhs.size() == girlCount){
break;
}
//随机
Collections.shuffle(familyNameList);
Collections.shuffle(girlList);
girlhs.add(familyNameList.get(0)+girlList.get(0));
}
ArrayList<String> list = new ArrayList<>();
Random r = new Random();
for (String boyName:boyhs){
int age = r.nextInt(10)+18;
list.add(boyName+"-男-"+ age);
}
// 4.生成女生的信息并添加到集合当中
for (String girlName:girlhs){
int age = r.nextInt(8)+18;
list.add(girlName+"-女-"+ age);
}
return list;
}
}
随机点名器
Student标准JavaBean类
package com.example.demo.myiotest5;
public class Student {
private String name;
private String gender;
private Integer age;
private double weight;
public Student() {
}
public Student(String name, String gender, Integer age, double weight) {
this.name = name;
this.gender = gender;
this.age = age;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public String toString() {
return name +"-" + gender +"-"+age+"-"+weight;
}
}
实现代码
package com.example.demo.myiotest5;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
public class Test {
public static void main(String[] args) throws IOException {
//1.把文件中的数据读取到内存中
ArrayList<Student> list = new ArrayList<>();
BufferedReader br = new BufferedReader(new FileReader("demo5\\src\\main\\java\\com\\example\\demo\\myiotest2\\names.txt"));
String line;
while ((line = br.readLine()) != null){
String[] arr = line.split("-");
Student student = new Student(arr[0],arr[1],Integer.parseInt(arr[2]),Double.parseDouble(arr[3]));
list.add(student);
}
br.close();
//2.计算权重的总和
double weight = 0;
for (Student student : list) {
weight += student.getWeight();
}
//3.计算每一个人的实际占比
double[] arr = new double[list.size()];
int index = 0;
for (Student student : list) {
arr[index] = student.getWeight()/weight;
index++;
}
//4.计算每一个人的权重占比范围
for (int i = 1; i < arr.length; i++) {
arr[i] += arr[i-1];
}
//5.随机抽取
// 获取一个0.0到1.0之间的随机数
double number = Math.random();
// 判断number在arr中的位置
// 二分查找法
// 方法返回:-插入点-1
// 获取number这个数据在数组当中的插入点位置
// 插入点 = -结果 - 1;
int result = -Arrays.binarySearch(arr, number)-1;
Student student = list.get(result);
System.out.println(student);
//6.修改当前学生的权重
double w = student.getWeight() / 2;
student.setWeight(w);
//7.把集合中的数据再次写到文件中
BufferedWriter bw = new BufferedWriter(new FileWriter("demo5\\src\\main\\java\\com\\example\\demo\\myiotest2\\names.txt"));
for (Student student1 : list) {
bw.write(student1.toString());
bw.newLine();
}
bw.close();
}
}
names.txt文件中的内容
登录
package com.example.demo.myiotest3;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws IOException {
// 1.读取正确的的用户名和密码
BufferedReader br = new BufferedReader(new FileReader("demo5\\src\\main\\java\\com\\example\\demo\\myiotest3\\userinfor.txt"));
String line = br.readLine();
br.close();
String[] userInfo = line.split("&");
String[] arr1 = userInfo[0].split("=");
String[] arr2 = userInfo[1].split("=");
String rightUsername = arr1[1];
String rightPassword = arr2[1];
// 2.用户键盘录入用户名和密码
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String username = sc.nextLine();
System.out.println("请输入密码");
String passWord = sc.nextLine();
// 3.比较
if(rightUsername.equals(username) && rightPassword.equals(passWord)){
System.out.println("登录成功");
}else{
System.out.println("登录失败");
}
}
}