首页 > 其他分享 >IO习题

IO习题

时间:2022-12-06 21:58:35浏览次数:26  
标签:String writer collect IO catch new 习题 txt

/**
* 一致student_info.txt文件中有一组数据按照格式【姓名-年龄-分数】存储
* 运用IO获取将该文件中的数据分别封装成5个student对象存入到集合中
* 要求根据学生的总分进行排序(降序),如果分数相同则比较年龄(降序)。并且输出展示
*/
private static void demo4() {
List students=new ArrayList<>(10);
try (
BufferedReader reader = new BufferedReader(new FileReader("student_info.txt"))
) {
String s;
while ((s=reader.readLine())!=null){
String[] split = s.split("-");
students.add(new Student(split[0],Integer.parseInt(split[1]),Integer.parseInt(split[2])));
}
List collect = students.stream().sorted(new Comparator() {
@Override
public int compare(Student o1, Student o2) {
return o2.getScore() - o1.getScore();
}
}.thenComparing(Student::getAge).reversed()).collect(Collectors.toList());
collect.forEach(System.out::println);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
 * 使用集合相关功能,存储10个数据(1~50的随机偶数),要求数字不能重复,添加完成后从大到小倒序遍历输出到控制台
 * 并且使用io流将集合中的元素按照指定格式输出到置顶文件中
 */
private static void demo3() {
    ThreadLocalRandom random = ThreadLocalRandom.current();
    Set<Integer> set = new HashSet<>(10);
    while (set.size() < 20) {
        int i = random.nextInt(1, 51);
        if (i % 2 == 0)
            set.add(i);
    }
    List<Integer> collect = set.stream().collect(Collectors.toList());
    Collections.sort(collect, ((Integer o1, Integer o2) -> o2 - o1));
    try (
            OutputStream stream = new FileOutputStream("e.txt")
    ) {
        String string = collect.toString();
        stream.write(string.getBytes());
        //[46, 44, 42, 40, 38, 36, 34, 32, 28, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4]
    } catch (FileNotFoundException e) {

    } catch (IOException e) {
        e.printStackTrace();
    }

}

/**
 * 读取任意txt文件内容,并统计出这个文本中每个字符以及每个字符出现的次数,并且以下格式:
 * 字符=次数,持久化保存在文件中
 */
private static void demo2() {
    Map<String, Integer> map = new HashMap<>(16);
    try (
            BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
            BufferedWriter writer = new BufferedWriter(new FileWriter("c.txt"))
    ) {
        int len;
        while ((len = reader.read()) != -1) {
            char a = (char) len;
            String s = String.valueOf(a);
            if (map.get(s) != null) {
                map.put(s, map.get(s) + 1);
            } else {
                map.put(s, 1);
            }
        }
        System.out.println(map);
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            writer.write(entry.getKey());
            writer.write("=");
            writer.write(String.valueOf(entry.getValue()));
            writer.newLine();
        }
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * 已知文件a.txt中的内容为:AAbcdea22dfewplkCC321ou1,
 * 请编写程序读取该文件内容,要求去掉重复字母,并且按照自然顺序后输出到b.txt
 */
private static void demo1() {
    try (
            BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
            BufferedWriter writer = new BufferedWriter(new FileWriter("b.txt"))
    ) {
        String s = reader.readLine();
        char[] chars = s.toCharArray();
        Arrays.sort(chars);
        s = new String(chars);
        s = s.replaceAll("(.)\\1+", "$1");
        writer.write(s);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

标签:String,writer,collect,IO,catch,new,习题,txt
From: https://www.cnblogs.com/Liku-java/p/16960660.html

相关文章

  • 实验六 模板类和文件IO
    任务四代码:Vector.hpp:#pragmaonce#include<iostream>usingnamespacestd;template<classT>classVector{public:Vector(intsize0):size{size......
  • CPP 如何将一个函数泛型化 How to Make a Function Generic
    1. Developinggenericfunction(设计泛型函数) 1.1.Steps(步骤) (1)    Tostartwithnon-genericfunction (先设计/编写一个非泛型函数)(2)   ......
  • 【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题
    问题描述在AzureFunctionTimerTrigger的函数中,添加了Singleton属性,当Function的实例变为3个后,发现Timer函数并没有在三个实例上同时运行,每次触发时,都只有在一个实例上......
  • 实验六 模板类和文件IO
    Task4//VEctor.hpp#include<bits/stdc++.h>usingnamespacestd;template<typenameT>classVector{private:intlength;T*base;publ......
  • poj1651 Multiplication Puzzle--区间dp
    原题链接:​​http://poj.org/problem?id=1651​​题意:给出N个数,每次从中抽出一个数(第一和最后一个不能抽),每次的得分为抽出的数与相邻两个数的乘积,直到只剩下首尾两个数为......
  • Spring框架之IOC入门
    1.开发环境IDEA版本:2022.1.4Maven版本:3.8.6Spring版本:6.0.2 2.案例分析2.1自定义Man类2.2自定义Company类2.3自定义CompanyTest类2.4分析图 3.案......
  • 1769.minimum-number-of-operations-to-move-all-balls-to-each-box 移动所有球到每个
    问题描述1769.移动所有球到每个盒子所需的最小操作数解题思路暴力求解,时间复杂度为\(\Theta(n^2)\);可以考虑利用前缀和来降低时间复杂度:设nums[i]是前i+1个盒子里......
  • Dissemination实验
    一、实验目的学习少量数据在传感网中可靠传递的方法掌握TinyOS中DisseminationValue、DisseminationUpdate接口和组件的使用理解Dissemination的机制 二、实......
  • 基础IO
    写在前面现在我们要开始Linux的第二个大模块,基础IO.在这里你将真正的认识到文件究竟是什么,了解到文件系统,更可以解决我们之前一直避而不谈的缓冲区,我们一起来看看吧.文......
  • 131.palindrome-partitioning 分割回文串
    问题描述131.分割回文串解题思路利用动态规划来判断字符串是否是回文串:-if(s[i]==s[j])dp[i][j]=dp[i+1][j-1];这里遍历的时候要注意i的遍历顺序;最后考......