JavaIO流简单练习题

吃猫的鱼
2023-05-29 / 0 评论 / 71 阅读 / 正在检测是否收录...

前言

今天需要使用JavaIO写一个简单的练习,现在将这个练习记录一下,防止日后忘记IO流相关语法。

相关知识

新增文件,并且向文件中写入数据,通过字符输出流,向文件中新增内容。

通过FileOutputStream fos = new FileOutputStream("number.txt")创建需要执行文件输出流的对象。

然后BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos))使用BufferedWriter使用缓冲输出流。

然后使用bw.write()向文件中写入内容。

try (FileOutputStream fos = new FileOutputStream("number.txt");
             BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos))){
            bw.write("需要向文件中写入的内容");
        } catch (Exception e) {
            e.printStackTrace();
           }

通过字符流读取文件中的内容

try(FileInputStream fis = new FileInputStream("number.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fis))){
            //字符流将文件中的第一行读取
            br.readLine()
        } catch (Exception e) {
            e.printStackTrace();
          }

题目

  1. 创建文件number.txt, 里面存放多个整数。
  2. 读取该文件里面的数据,并统计平均值,最大值,最小值。
  3. 将统计的信息及数据排序后,写入result.txt

解决问题

问题分析:首先需要随机生成十个整数,将其放入文件中,然后程序再通过读取文件,获取到文件里面的十个数据,然后计算其平均值、最大最小值,最后将排序后的结果放入文件中

public static void main(String[] args) {
        Random rand = new Random();
        //接下来开始自动生成文件,并且系统生成随机数,每次运行程序新建/覆盖文件中的数
        try (FileOutputStream fos = new FileOutputStream("number.txt");
             BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos))){
            //随机生成十个0-100整数,存放到文件中
            for (int i = 0; i < 10; i++) {
                bw.write(rand.nextInt(101) + " ");
            }
            System.out.println("文件创建成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }

        //接下来开始通过字符流读取文件中的内容
        String numData[];
        ArrayList<Integer> intData = new ArrayList<>();
        try(FileInputStream fis = new FileInputStream("number.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fis))){
            //字符流将文件中的数据读取到numData中
            numData = br.readLine().split(" ");
            //遍历将读取到的String类型的数据转换类型,并且放入arraylist中
            for (String num:numData) {
                intData.add(Integer.parseInt(num));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //接下来将读取到结果放入到result.txt
        try (FileOutputStream fos = new FileOutputStream("result.txt");
             BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos))){
            bw.write("平均值:"+mathOperate(intData,0)+"\n");
            bw.write("最大值:"+mathOperate(intData,1)+"\n");
            bw.write("最小值:"+mathOperate(intData,-1)+"\n");
            //排序后写入
            Collections.sort(intData);
            bw.write(intData.toString());
            System.out.println("创建结果文件成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 本函数用于执行对整型集合的相关数学操作,包括最大值、最小值、平均值
     * @param arrayList     传入集合
     * @param type          0表示平均值计算,1表示最大值搜索,-1表示最小值搜索
     * @return              返回结果
     */
    public static float mathOperate(ArrayList<Integer> arrayList,int type){
        if(type==0){                    //计算平均值
            int sum = 0;
            for (Integer num:arrayList) {
                sum+=num;
            }
            return (float)sum/arrayList.size();
        }else {
            //通过排序获取最大最小值
            Collections.sort(arrayList);
            if (type==-1){              //返回最小值
                return arrayList.get(0);
            }else if(type==1){          //返回最大值
                return arrayList.get(arrayList.size()-1);
            }
        }
        return Float.MIN_VALUE;         //如果传入的数不为0,1,2 返回浮点数最小值。
    }

执行结果:

result.txt

result.txt

number.txt

number.txt


1

评论 (0)

取消
友情链接 文章阅读: 网站地图