Java笔记_19(IO综合练习)

Java笔记_19

  • 一、综合练习
    • 1.1、网络爬取(数据处理并写出)
    • 1.2、利用胡涂包生成数据
    • 1.3、随机点名器的四个需求
    • 1.4、带权重的随机算法

一、综合练习

1.1、网络爬取(数据处理并写出)

Java笔记_19(IO综合练习)_第1张图片

package IO_LastTest;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Dome1 {
    public static void main(String[] args) throws IOException {
        //获取要爬取的地址
        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&qq-pf-to=pcqq.c2c";
        String BoyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String GrilNameNet = "http://www.haoming8.cn/baobao/7641.html";
        //调用爬取函数
        String familyName = getNet(familyNameNet);
        String BoyName = getNet(BoyNameNet);
        String GrilName = getNet(GrilNameNet);
        //通过正则表达式提取想要获取的数据
        String familyNameregex = "(.{4})(,|。)";
        ArrayList<String> familyNameTemplist = getString(familyName, familyNameregex, 1);
        // /^[\u4E00-\u9FA5]+$/
        String BoyNameregex = "([\\u4E00-\\u9FA5]{2})(、)";
        ArrayList<String> BoyNameTempList = getString(BoyName,BoyNameregex,1);
        String GrilNameregex = "([\\u4E00-\\u9FA5]{2})(\\s)";
        ArrayList<String> GrilNameTempList = getString(GrilName, GrilNameregex, 1);

        ArrayList<String> familyNameList = new ArrayList<>();
        for (String s : familyNameTemplist) {
            char[] chars = s.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                familyNameList.add(chars[i]+"");
            }
        }

        ArrayList<String> BoyNameList = new ArrayList<>();
        for (String s : BoyNameTempList) {
            if (!BoyNameList.contains(s)){
                BoyNameList.add(s);
            }
        }

        ArrayList<String> GrilNameList = new ArrayList<>();
        for (String s : GrilNameTempList) {
            if (!GrilNameList.contains(s)){
                GrilNameList.add(s);
            }
        }
        ArrayList<String> name = getName(familyNameList, BoyNameList, GrilNameList, 50, 20);
        Collections.shuffle(name);


        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\Java\\IDEA\\代码文件\\After_pintu_again\\a.txt"));
        for (String s : name) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();

    }

    //将男女生姓名包装好放到集合中
    public static ArrayList<String> getName(ArrayList<String> familyNameList,ArrayList<String> BoyNameList,ArrayList<String> GrilNameList,int boyNum,int grilNum){
        ArrayList<String> boyname = new ArrayList<>();
        while (true){
            if (boyname.size()==boyNum){
                break;
            }
            Collections.shuffle(familyNameList);
            Collections.shuffle(BoyNameList);
            boyname.add(familyNameList.get(0)+BoyNameList.get(0));
        }

        ArrayList<String> grilname = new ArrayList<>();
        while (true){
            if (grilname.size()==grilNum){
                break;
            }
            Collections.shuffle(familyNameList);
            Collections.shuffle(GrilNameList);
            grilname.add(familyNameList.get(0)+GrilNameList.get(0));
        }

        ArrayList<String> list = new ArrayList<>();
        Random r = new Random();
        for (String s : boyname) {
            int age = r.nextInt(10)+18;
            list.add(s+"-男-"+age);
        }
        for (String s : grilname) {
            int age = r.nextInt(8)+18;
            list.add(s+"-女-"+age);
        }
        return list;
    }

    /*
    作用:将爬到的信息切割好之后装到集合中
    * 参数一:
    *       需要进行获取数据的网址字符串
       参数二:
            正则表达式规则
       参数三:
            用正则表达式划分后获取的某一组数据
    * */
    public static ArrayList<String> getString(String name,String regex,int i){
        //先创建一个集合存放数据
        ArrayList<String> list = new ArrayList<>();
        //按照正则表达式的规则获取数据
        Pattern pattern = Pattern.compile(regex);
        //字符串按照正则表达式的方式分割
        Matcher matcher = pattern.matcher(name);
        while (matcher.find()){
            list.add(matcher.group(i));
        }
        return list;
    }
    /*
    * 参数一:
    *       网络地址
    * */
    public static String getNet(String str) throws IOException {
        //定义一个容器拼接爬取的字符串
        StringBuilder sb =new StringBuilder();
        //创建一个URL对象
        URL url = new URL(str);
        //连接上这个网址
        //保证网络畅通
        URLConnection coon = url.openConnection();
        //读取数据
        InputStreamReader isr = new InputStreamReader(coon.getInputStream());
        int len;
        while ((len=isr.read())!=-1){
            sb.append((char) len);
        }
        isr.close();
        return sb.toString();
    }
}

1.2、利用胡涂包生成数据

  • 胡涂包里面的使用规则
//请求列表页
String listContent = HttpUtil.get("https://www.oschina.net/action/ajax/get_more_news_list?newsType=&p=2");
//使用正则获取所有标题
List<String> titles = ReUtil.findAll("(.*?)", listContent, 1);
for (String title : titles) {
    //打印标题
    Console.log(title);
}
package IO_LastTest;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class Dome2 {
    public static void main(String[] args) throws IOException {
        //获取要爬取的地址
        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&qq-pf-to=pcqq.c2c";
        String BoyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String GrilNameNet = "http://www.haoming8.cn/baobao/7641.html";
        //爬取地址里面的数据
        String familyNamestr = HttpUtil.get(familyNameNet);
        String BoyNamestr = HttpUtil.get(BoyNameNet);
        String GrilNamestr = HttpUtil.get(GrilNameNet);
        //按照正则表达式的方式将爬取的数据进行切割
        List<String> familyNameTemplist = ReUtil.findAll("(.{4})(,|。)", familyNamestr, 1);
        List<String> BoyNameTempList = ReUtil.findAll("([\\u4E00-\\u9FA5]{2})(、)", BoyNamestr, 1);
        List<String> GrilNameTempList = ReUtil.findAll("([\\u4E00-\\u9FA5]{2})(\\s)",GrilNamestr,1);

        ArrayList<String> familyNameList = new ArrayList<>();
        for (String s : familyNameTemplist) {
            char[] chars = s.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                familyNameList.add(chars[i]+"");
            }
        }

        ArrayList<String> BoyNameList = new ArrayList<>();
        for (String s : BoyNameTempList) {
            if (!BoyNameList.contains(s)){
                BoyNameList.add(s);
            }
        }

        ArrayList<String> GrilNameList = new ArrayList<>();
        for (String s : GrilNameTempList) {
            if (!GrilNameList.contains(s)){
                GrilNameList.add(s);
            }
        }
        ArrayList<String> name = getName(familyNameList, BoyNameList, GrilNameList, 50, 20);
        Collections.shuffle(name);
        //"D:\\Java\\IDEA\\代码文件\\After_pintu_again\\b.txt"

        //写出数据
        FileUtil.writeLines(name,"D:\\Java\\IDEA\\代码文件\\After_pintu_again\\b.txt","UTF-8");
    }
    public static ArrayList<String> getName(ArrayList<String> familyNameList,ArrayList<String> BoyNameList,ArrayList<String> GrilNameList,int boyNum,int grilNum){
        ArrayList<String> boyname = new ArrayList<>();
        while (true){
            if (boyname.size()==boyNum){
                break;
            }
            Collections.shuffle(familyNameList);
            Collections.shuffle(BoyNameList);
            boyname.add(familyNameList.get(0)+BoyNameList.get(0));
        }

        ArrayList<String> grilname = new ArrayList<>();
        while (true){
            if (grilname.size()==grilNum){
                break;
            }
            Collections.shuffle(familyNameList);
            Collections.shuffle(GrilNameList);
            grilname.add(familyNameList.get(0)+GrilNameList.get(0));
        }

        ArrayList<String> list = new ArrayList<>();
        Random r = new Random();
        for (String s : boyname) {
            int age = r.nextInt(10)+18;
            list.add(s+"-男-"+age);
        }
        for (String s : grilname) {
            int age = r.nextInt(8)+18;
            list.add(s+"-女-"+age);
        }
        return list;
    }

}

1.3、随机点名器的四个需求

Java笔记_19(IO综合练习)_第2张图片

package IO_LastTest;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class Dome3 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\Java\\IDEA\\代码文件\\After_pintu_again\\b.txt"));

        ArrayList<String> list = new ArrayList<>();
        String len;
        while ((len = br.readLine())!=null){
            list.add(len);
        }
        br.close();
        Random r = new Random();
        int index = r.nextInt(list.size());

       String str =  list.get(index).split("-")[0];
       System.out.println(str);
    }
}

Java笔记_19(IO综合练习)_第3张图片

package IO_LastTest;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Dome4 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\Java\\IDEA\\代码文件\\After_pintu_again\\b.txt"));
        //定义两个集合分别装载男生和女生的姓名
        ArrayList<String> manlist = new ArrayList<>();
        ArrayList<String> womanlist = new ArrayList<>();
        String str;
        while ((str = br.readLine())!=null){
            //将字符串切割对比之后取出来分别是男女装到两个不同的集合中
            if (str.split("-")[1].equals("男")){
                manlist.add(str);
            }else {
                womanlist.add(str);
            }
        }
        br.close();
        //System.out.println(manlist);
        //System.out.println(womanlist);
        //定义一个integer型集合装载1和0其中比例为7:3,通过取出中的数字来判断是取女生姓名还是男生姓名
        ArrayList<Integer> integers = new ArrayList<>();
        Collections.addAll(integers,1,1,1,1,1,1,1);
        Collections.addAll(integers,0,0,0);

        Random r = new Random();
        int index = r.nextInt(integers.size());
        //如果取出的是1则取出男生集合里的数据
        if (integers.get(index)==1){
            Collections.shuffle(manlist);
            System.out.println(manlist.get(0).split("-")[0]);
        } else if (integers.get(index)==0) {
            //如果取出的是2则取出女生集合里的数据
            Collections.shuffle(womanlist);
            System.out.println(womanlist.get(0).split("-")[0]);
        }

    }
}

Java笔记_19(IO综合练习)_第4张图片

package IO_LastTest;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class Dome5 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("b.txt"));
        //先取出文件中所有的数据加载到集合中
        ArrayList<String> list = new ArrayList<>();
        String str;
        while ((str = br.readLine())!=null){
            list.add(str);
        }
        br.close();
        //定义c.txt文件定义次数变量,当达到第三次时做出作弊事件
        BufferedReader br2 = new BufferedReader(new FileReader("c.txt"));
        int count = Integer.parseInt(br2.readLine());
        count++;
        BufferedWriter bw = new BufferedWriter(new FileWriter("c.txt"));
        bw.write(count+"");
        br2.close();
        bw.close();
        if (count==3){
            System.out.println("张三");
        }else {
            Collections.shuffle(list);
            System.out.println(list.get(0).split("-")[0]);
        }

    }
}

Java笔记_19(IO综合练习)_第5张图片

package IO_LastTest;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class Dome6 {
    public static void main(String[] args) throws Exception {
        File f1 = new File("b.txt");
        File f2 = new File("d.txt");
        ArrayList<String> list1 = new ArrayList<>();

        BufferedReader br = new BufferedReader(new FileReader(f1));
        String len;
        while ((len = br.readLine())!=null){
            list1.add(len);
        }
        br.close();
        //打乱集合的顺序,获取第一个数据
        Collections.shuffle(list1);
        String name =  list1.get(0).split("-")[0];
        System.out.println(name);
        BufferedWriter bw2 = new BufferedWriter(new FileWriter(f1));
        BufferedWriter bw = new BufferedWriter(new FileWriter(f2,true));
        //每次得到一个数据就添加到另一个文件中,并且删除该数据在集合中
        bw.write(list1.get(0));
        bw.newLine();
        list1.remove(0);

        for (String s : list1) {
            bw2.write(s);
            bw2.newLine();
        }
        bw.close();
        bw2.close();

        System.out.println("==============");
        //如果集合的长度为零了,就将文件2的数据copy到文件1中,并且清空文件2
        if (list1.size()==0){
            FileUtils.copyFile(f2,f1);
            writeFile(f2);
        }

    }
    public static void writeFile(File f2) throws Exception{
        FileWriter fw = new FileWriter (f2);
        fw.write("");
        fw.flush();
        fw.close();
    }
}

1.4、带权重的随机算法

Java笔记_19(IO综合练习)_第6张图片

package IO_LastTest;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class Dome7 {
    public static void main(String[] args) throws IOException {
        ArrayList<Student> list = new ArrayList<>();

        BufferedReader br = new BufferedReader(new FileReader("Dome7.txt"));
        String len;
        while ((len = br.readLine())!=null){
            String[] split = len.split("-");
            list.add(new Student(split[0],split[1],Integer.parseInt(split[2]),Double.parseDouble(split[3])));
        }
        br.close();
        System.out.println(list);

        //计算权重总和
        double weight = 0;
        for (Student student : list) {
            weight = weight + student.getWeight();
        }

        //计算每一个人的实际占比
        double[] arr = new double[list.size()];
        int index = 0;
        for (Student student : list) {
            arr[index] = student.getWeight()/weight;
            index++;
        }
        //计算每一个人的权重范围
        for (int i = 1; i < arr.length; i++) {
            arr[i] = arr[i] + arr[i-1];
        }
        System.out.println(Arrays.toString(arr));

        //随机抽取
        //获取一个0.0~1.0之间的随机数
        double number = Math.random();
        //二分查找法
        //方法的返回值是 ( -插入点 - 1)
        int result  = -Arrays.binarySearch(arr,number) - 1;

        Student student = list.get(result);
        System.out.println(student);
        double sw =  student.getWeight() / 2;
        student.setWeight(sw);

        //再把集合中的数据打印到文件中
        BufferedWriter bw = new BufferedWriter(new FileWriter("Dome7.txt"));
        for (Student student1 : list) {
            bw.write(student1.toString());
            bw.newLine();
        }
        bw.close();
    }
}

你可能感兴趣的:(Java,java,笔记,开发语言)