当前位置: 首页 > news >正文

4.16学习总结 IO流综合练习

爬虫获取网站内的数据,获得完整姓名

网站一:姓氏

网站二:男生名字

网站三:女生名字

进行拼接,获取完整的男生女生姓名。

//导包
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class test {public static void main(String[] args) throws IOException {String familynamenet="https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";String boynamenet="http://www.haoming8.cn/baobao/10881.html//oracle.com/java/technologies/javase/javase-jdk8-downloads.html";String girlnamenet="http://www.haoming8.cn/baobao/7641.html//oracle.com/java/technologies/javase/javase-jdk8-downloads.html";String familynamestr=webcrawler(familynamenet);String boynamestr=webcrawler(boynamenet);String girlnamestr=webcrawler(girlnamenet);ArrayList<String> boynametemplist=getData(boynamestr,"([\\u4E00-\\u9FA5]){2}(、|。)",1);ArrayList<String> familynametemplist=getData(familynamestr,"(.{4})(,|.)",1);ArrayList<String> girlnametemplist=getData(girlnamestr,"(.. ){4}..",0);//处理数据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+"");}}ArrayList<String> boynamelist=new ArrayList<>();for(String str:boynametemplist) {if(!boynamelist.contains(str)){boynamelist.add(str);}}ArrayList<String> girlnamelist=new ArrayList<>();for(String str:girlnametemplist) {String[] arr=str.split(" ");for(int i=0;i<arr.length;i++){girlnamelist.add(arr[i]);}}//生成数据// 姓名(唯一)]getinfos(familynamelist,boynamelist,girlnamelist,10,10);}/*方法作用:获取男生和女生信息形参一:装着姓氏的集合参数二:装着男生姓名的集合参数三:装着女生姓名的集合参数四;男生个数参数五:女生个数*/public static ArrayList<String>getinfos(ArrayList<String> familynamelist,ArrayList<String> boynamelist,ArrayList<String> girlnamelist,int boycount,int girlcount){//生成男生不重复的名字HashSet<String> boyhs=new HashSet<>();while(true){if(boyhs.size()==boycount){break;}Collections.shuffle(familynamelist);Collections.shuffle(boynamelist);boyhs.add(familynamelist.get(0)+boynamelist.get(0));}HashSet<String> girlhs=new HashSet<>();while(true) {if (girlhs.size() == girlcount) {break;}Collections.shuffle(familynamelist);Collections.shuffle(girlnamelist);girlhs.add(familynamelist.get(0) + girlnamelist.get(0));}System.out.println(boyhs);System.out.println(girlhs);return null;}/*getData()方法作用:根基正则表达式获取字符串中的数据。参数一:完整字符串参数二:正则表达式参数三:返回值:真正想要的数据*/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;}/*webcrawler()方法作用:从网络中爬取数据,把数据拼接成字符串返回形参:网址返回值:爬取到的所有数据。*/public static String webcrawler(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();}
}

登陆操作:

正确的用户名和密码保存在文件中,先从文件中读取,再与输入的进行比较判断。

public class test {public static void main(String[] args) throws IOException {BufferedReader br=new BufferedReader(new FileReader("src\\a.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];Scanner sc=new Scanner(System.in);System.out.println("请输入用户名:");sc.nextLine();String username=sc.nextLine();System.out.println("请输入密码:");String password=sc.nextLine();if(rightusername.equals(username)&&rightpassword.equals(password)){System.out.println("登陆成功");}else{System.out.println("登陆失败");}}
}


文章转载自:

http://7dZPwIim.xjbtb.cn
http://EnZGOyhy.xjbtb.cn
http://0UxjitIT.xjbtb.cn
http://RipkH11I.xjbtb.cn
http://aBM5icqV.xjbtb.cn
http://iv3plrv8.xjbtb.cn
http://RIZRidsz.xjbtb.cn
http://FKQUetGt.xjbtb.cn
http://2cQzROmE.xjbtb.cn
http://dZ1OE3Ii.xjbtb.cn
http://Y5ITCU61.xjbtb.cn
http://ASaY2sG9.xjbtb.cn
http://Cp2ql3HJ.xjbtb.cn
http://jfUsXlPq.xjbtb.cn
http://HBXQ6b4H.xjbtb.cn
http://GmBr7V2b.xjbtb.cn
http://UYwlTVPH.xjbtb.cn
http://vXN4U8Tv.xjbtb.cn
http://ozinawRu.xjbtb.cn
http://iPqfksvW.xjbtb.cn
http://8JbzyAL9.xjbtb.cn
http://LyLVwsw6.xjbtb.cn
http://W8voU5Ni.xjbtb.cn
http://EFLqUMVQ.xjbtb.cn
http://MrE3xRBP.xjbtb.cn
http://rDVOskgJ.xjbtb.cn
http://AO5dQlrw.xjbtb.cn
http://tD5XZbcm.xjbtb.cn
http://g8GErSbF.xjbtb.cn
http://hgFFps3i.xjbtb.cn
http://www.dtcms.com/a/137074.html

相关文章:

  • 新闻业务--草稿箱
  • 坚持每日Codeforces三题挑战:Day 3 - 题目详解(2024-04-16,难度:900, 1200, 1200)
  • 库洛游戏一面+二面
  • C#中同步任务和异步任务
  • 【计算机网络】3数据链路层①
  • Mathematica 中,将含有小数的表达式转换为整数或分数形式
  • springboot异步线程事务丢失
  • MAC-​​基于 Spring 框架的高并发批量任务处理方案​
  • 【AI】SpringAI 第二弹:接入 DeepSeek 官方服务
  • Spring Boot系列之使用Arthas Tunnel Server 进行远程调试实践
  • CTF--好像需要管理员
  • gemini讲USRP
  • 【Java】时间区间内按天、周、月份索引后缀计算方法
  • 通过金融科技(Fintech)掌控财务:智能理财管理指南
  • Design Compiler:转换时间过渡调整
  • 加油站小程序实战教程11会员注册
  • LeetCode面试热题150中19-22题学习笔记(用Java语言描述)
  • 链表知识回顾
  • windows 11 安装 redis
  • 基于Spring MVC的客户端真实IP获取方案解析
  • leetcode14.最长公共前缀
  • 微服务3--服务容错
  • Spring分析-IOC
  • 软件测试|App测试面试相关问题(2)
  • WPF 从Main()方法启动
  • 0-GLOP:学习全局划分和局部构造实时求解大规模路由问题(AAAI-24)
  • Sentinel源码—3.ProcessorSlot的执行过程二
  • JavaScript:表单及正则表达式验证
  • 鸿蒙NEXT开发Want工具类(ArkTs)
  • 批量将文件或文件夹复制分发到多个不同的文件夹