不必每分钟都学习,但求学习中每分钟都有收获。

IO流

概念

IO流用来处理设备之间的数据传输Java对数据的操作是通过流的方式Java用于操作流的类都在IO包中。
流按流向分为两种:输入流,输出流。

流按操作类型分为两种:
字节流 : 字节流可以操作任何数据,因为在计算机中任何数据都是以字节的形式存储的
字符流 : 字符流只能操作纯字符数据,比较方便。

IO流常用父类
     字节流的抽象父类:
         InputStream 
         OutputStream
     字符流的抽象父类:
         Reader 
         Writer

IO流常用类的体系图

 

关于IO流的流的效率的代码实例

基本流的一次写一个字符

public class Demo5 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("a.txt");
        FileOutputStream out = new FileOutputStream("b.txt");
        int len=0;
        while ((len=in.read())!=-1){
            out.write(len);
            out.flush();
        }
        in.close();
        out.close();
    }
}

基本流的一次一个字符数组

public class Demo5 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("a.txt");
        FileOutputStream out = new FileOutputStream("b.txt");
        byte[] bytes = new byte[1024];
        int len=0;
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
            out.flush();
        }
        in.close();
        out.close();
    }

}

 高效的流一次一个字符

public class Demo5 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("a.txt"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("n.txt"));
        int len=0;
        byte[] bytes = new byte[1024];
        while ((len=bufferedInputStream.read(bytes))!=-1){
            bufferedOutputStream.write(bytes,0,len);
            bufferedOutputStream.flush();
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }
}

高效的流一次一个字符数组

public class Demo5 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("a.txt"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("n.txt"));
        int len=0;
        byte[] bytes = new byte[1024];
        while ((len=bufferedInputStream.read(bytes))!=-1){
            bufferedOutputStream.write(bytes,0,len);
            bufferedOutputStream.flush();
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
    }
}

通过上述的实力,我们会发现,字符数组比单个字符的效率要高,高效的流又比普通流的效率要高。

 

具体实例结合

前面两片博客学了map与set,现在我们就总共一起来做一个例子

实例:键盘录入3个学生信息(姓名,语文成绩,数学成绩,英语成绩)

           按照总分从高到低存入文本文件

首先我们要创一个学生类出来

public class Student {
    private String name;
    private int chineseScore;
    private int mathScore;
    private int englishScore;

    public String getName() {
        return name;
    }

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

    public int getChineseScore() {
        return chineseScore;
    }

    public void setChineseScore(int chineseScore) {
        this.chineseScore = chineseScore;
    }

    public int getMathScore() {
        return mathScore;
    }

    public void setMathScore(int mathScore) {
        this.mathScore = mathScore;
    }

    public int getEnglishScore() {
        return englishScore;
    }

    public void setEnglishScore(int englishScore) {
        this.englishScore = englishScore;
    }
    public int getTotalScore(){
        return chineseScore+mathScore+englishScore;
    }
}

接下来,我们开始存储学生类,将学生信息存储在文本文件中

public class Demo6 {
    public static void main(String[] args) throws IOException {
      
        Scanner scanner = new Scanner(System.in);
        TreeSet<Student> students = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
 
                int i = s1.getTotalScore() - s2.getTotalScore();
                int num = i == 0 ? s1.getName().compareTo(s2.getName()) : i;
                return num;
            }
        });
        for (int i = 1; i <= 3; i++) {
            Student student = new Student();
            System.out.println("请输入第" + i + "个学生姓名");
            String name = scanner.next();
            student.setName(name);
            System.out.println("请输入第" + i + "个学生语文成绩");
            int chineseScore = scanner.nextInt();
            student.setChineseScore(chineseScore);
            System.out.println("请输入第" + i + "个学生数学成绩");
            int mathScore = scanner.nextInt();
            student.setMathScore(mathScore);
            System.out.println("请输入第" + i + "个学生英语成绩");
            int englishSocre = scanner.nextInt();
            student.setEnglishScore(englishSocre);
            students.add(student);
        }

 
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("xuesheng.txt", true)));
   
        writer.write("姓名 语文成绩 数学成绩 英语成绩 总分");
        writer.newLine();
        writer.flush();
        for (Student student : students) {
            String name = student.getName();
            int chineseScore = student.getChineseScore();
            int mathScore = student.getMathScore();
            int englishScore = student.getEnglishScore();
            int totalScore = student.getTotalScore();
            writer.write(name + "   " + chineseScore + "     " + mathScore + "      " + englishScore + "      " + totalScore);
            writer.newLine();
            writer.flush();
        }
        writer.close();

    }
}

最后通过自己输入数据输出结果:

姓名 语文成绩 数学成绩 英语成绩 总分
李四   85     89      56      230
李华   96     95      56      247
张三   88     85      87      260