JAVA实验7 输入输出流I/O

实验7 输入输出流I/O

实验要求:
(1)掌握File类;
(2)掌握常用字节流类;
(3)掌握常用的字符流及缓冲流的使用。
实验内容:
(1)按行读取文件studentInfo.txt的内容,获取学生姓名并写入到studentName.txt文件中,且给每一行按序加上行号;获取studentName.txt文件的长度及修改时间。
studentInfo.txt文件内容格式如下:
王珊珊,王珊珊,20132213806,2015-10-10 14:30:56
张国辉,张国辉,20132213944,2015-10-10 14:31:05
蒋宇宙,蒋宇宙,20132213906,2015-10-10 14:31:24
王燕萍,王燕萍,20132213847,2015-10-10 14:31:16

import java.io.*;
import java.text.SimpleDateFormat;

public class StudentIO1 {
    private static final String fr ="test7/src/studentInfo.txt";
    private static final String fw ="test7/src/studentName.txt";

    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new FileReader(fr));
             BufferedWriter bw = new BufferedWriter(new FileWriter(fw));
             ){
            String line;
            Integer cnt = 1;
            while ((line = br.readLine()) != null){
                String[] arr = line.split(","); //根据逗号分割
                bw.write(  cnt.toString()+"、"+arr[0]);
                bw.newLine();
                cnt++;
//                System.out.println(line);
            }
            File f = new File(fw);
            System.out.println("studentName.txt文件的长度:"+f.length());
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("studentName.txt文件的修改时间:"+time.format(f.lastModified()));

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

在这里插入图片描述
在这里插入图片描述

(2)学生成绩存储在文件中,每行一个学生成绩,形如:
20151201 85 84 91.5
20151202 70 85 65

请编程读入学生成绩,并计算各门课程的均值和方差,写入文件的最后一行,如:
均值和方差 71/10 81.2/11.3 76/15.3

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

public class StudentGradeIO {
    private static final String f ="test7/src/studentGrade.txt";

    public static void main(String[] args) {

        try(BufferedReader br1 = new BufferedReader(new FileReader(f));
            BufferedReader br2 = new BufferedReader(new FileReader(f));
            BufferedWriter bw = new BufferedWriter(new FileWriter(f, true))
        ) {
            String line;
            int n = 0;  //总人数
            String[] arr = null;
            ArrayList<Double> sc = new ArrayList<>(100);   //存放每一科成绩的平均分
             ArrayList<Double> var = new ArrayList<>(100);   //存放每一科成绩的方差
            for (int i = 0; i < 100; i++){  //初始化
                sc.add(0.0);
                var.add(0.0);
            }

            while ((line = br1.readLine()) != null)
            {
                arr = line.replaceAll(" +", " ").split(" "); //根据空格分割
                for (int i = 1; i < arr.length; i++) {
                    sc.set(i, sc.get(i) + Double.parseDouble(arr[i]));
//                    System.out.print(sc.get(i)); System.out.print("  ");
                }
                n++;
            }
            for (int i = 1; i < arr.length; i++)    //求每科的平均分
                sc.set(i, sc.get(i) / n);

            //求方差
            while ((line = br2.readLine()) != null)
            {
                 arr = line.replaceAll(" +", " ").split(" "); //根据空格分割
                 for (int i = 1; i < arr.length; i++) {
                     var.set(i, var.get(i) + (Double.parseDouble(arr[i]) - sc.get(i)) * (Double.parseDouble(arr[i]) - sc.get(i)));
                 }
            }
            bw.newLine();
            bw.write("均值和方差");
            for (int i = 1; i < arr.length; i++)    //求每科的方差---并写入文件
            {
                var.set(i, var.get(i) / n);
                bw.write("\t"+String.valueOf(sc.get(i))+"/"+String.valueOf(var.get(i)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java输入输出流(I/O)提供了在程序读取和写入数据的方式。它们是通过处理流(或过滤器流)的方式工作的,这些流允许你对数据进行处理和转换。 常用的输入输出流有: 1. FileInputStream:用于从文件读取字节流。 2. FileOutputStream:用于向文件写入字节流。 3. FileReader:用于从文件读取字符流。 4. FileWriter:用于向文件写入字符流。 5. ByteArrayInputStream:用于从内存读取字节流。 6. ByteArrayOutputStream:用于向内存写入字节流。 7. CharArrayReader:用于从字符数组读取字符流。 8. CharArrayWriter:用于向字符数组写入字符流。 9. StringReader:用于从字符串读取字符流。 10. StringWriter:用于向字符串写入字符流。 下面是一个简单的示例,演示了如何使用FileInputStream和FileOutputStream来复制一个文件: ```java import java.io.*; public class FileCopyDemo { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("input.txt"); out = new FileOutputStream("output.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } } ``` 这个程序将从名为input.txt文件读取字节,并将它们写入名为output.txt文件。在读取和写入完成后,程序会关闭这两个流。 需要注意的是,在使用输入输出流时,要确保正确地处理异常和关闭流。可以使用try-with-resources语句来简化这个过程,如下所示: ```java try (FileInputStream in = new FileInputStream("input.txt"); FileOutputStream out = new FileOutputStream("output.txt")) { int c; while ((c = in.read()) != -1) { out.write(c); } } catch (IOException e) { e.printStackTrace(); } ``` 在此示例,使用了try-with-resources语句来创建和管理输入输出流。当try块结束时,会自动关闭这两个流,从而避免了显式地关闭流的需要。 除了上述示例使用的流之外,Java 还提供了许多其他的输入输出流,以满足不同的需求。你可以根据需要选择适合自己的流。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码不会敲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值