Java教程

Java-IO练习题

本文主要是介绍Java-IO练习题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

练习一:

package com.shujia.wyh.day25;
import java.io.*;
import java.util.Arrays;
/*
    已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”
    请编写程序读取数据内容,把数据排序后写入ss.txt中。

 */
public class IOTest1 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\IdeaProjects\\bigdata17\\src\\com\\shujia\\wyh\\day25\\s.txt"));

        //由于只有一行数据,就只需要读一次readLine就可以了
        String line = br.readLine();

        //将字符串转成字符数组
        byte[] bytes = line.getBytes();

        //使用Arrays工具类对数组进行排序
        Arrays.sort(bytes);

        //再将数组转成字符串
        String s = new String(bytes);

        //创建字符输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\IdeaProjects\\bigdata17\\src\\com\\shujia\\wyh\\day25\\ss.txt"));
        bw.write(s);
        bw.flush();

        //释放资源
        bw.close();
        br.close();


    }
}

练习二:

package com.shujia.wyh.day24;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/*
    从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合

 */
public class IOTest2 {
    public static void main(String[] args) throws IOException {
        //创建字符缓冲输入流
        BufferedReader br = new BufferedReader(new FileReader("io1.txt"));
        //创建一个集合对象
        ArrayList<String> list = new ArrayList<>();

        //一次读取一行
        String line = null;
        while ((line=br.readLine())!=null){
            //将一行的数据作为一个元素添加到集合中
            list.add(line);
        }

        System.out.println(list);



        //释放资源
        br.close();

    }
}

练习三:

package com.bigdat.java.day25;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
/*
      键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
 */
public class ListAndFileText1 {
    public static void main(String[] args) {
        //创建键盘录入对象
        Scanner scanner = new Scanner(System.in);
        //创建学生类集合
        TreeSet<Student> ts = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //首先比较第一要素:总成绩
                int i = o2.getAll()-o1.getAll();
                //其次比较第二要素:语文
                int i1 = (i == 0)? o2.getChinese()-o1.getChinese():i;
                int i2 = (i1 == 0)? o2.getMath()-o1.getMath():i1;
                int i3 = (i2 == 0)? o2.getEnglish()-o1.getEnglish():i2;
                return i3;
            }
        });

        //因为要输入五个学生信息;选择 for 循环
        //创建学生对象
        Student student = null;
        for (int i = 1; i <= 5; i++) {
            System.out.println("请输入第"+i+"个学生的姓名:");
            String name = scanner.next();
            System.out.println("请输入该学生的语文成绩:");
            int chinese = scanner.nextInt();
            System.out.println("请输入该学生的数学成绩:");
            int math = scanner.nextInt();
            System.out.println("请输入该学生的英语成绩:");
            int english = scanner.nextInt();

            student = new Student(name,chinese,math,english);
             //将学生信息写入到集合中
            ts.add(student);
            System.out.println("-------------------------");
        }
        //创建字符输出流对象
        BufferedWriter br = null;
        //遍历学生集合
        StringBuffer sb = null;
        for (Student t : ts) {
            String name = t.getName();
            int chinese = t.getChinese();
            int math = t.getMath();
            int english = t.getEnglish();
            sb = new StringBuffer();
            sb.append("学生姓名:").append(name)
               .append(",语文成绩:").append(chinese)
                .append(",数学成绩:").append(math)
                .append(",英语成绩:").append(english)
                 .append(",总成绩:").append(t.getAll());
            String str = sb.toString();

          try {
            br = new BufferedWriter(new FileWriter("D:\\student.txt",true));
            //写入学生信息;
              br.write(str);
              br.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        }
        System.out.println("计算成绩中,请稍后...");
    }
}

学生类

package com.bigdat.java.day25;

public class Student {
    private String name;
    private int chinese;
    private int math;
    private int english;
    private int all;

    public Student() {
    }

    public Student(String name, int chinese, int math, int english) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    public int getAll() {
       return this.all = this.chinese+this.math+this.english;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", chinese=" + chinese +
                ", math=" + math +
                ", english=" + english +
                '}';
    }
}

这篇关于Java-IO练习题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!