Java面向对象---IO流(基本流和缓冲流)

一、IO流的分类

1、文件类型来分类
    字符流:专门用来读取写入文本文件的
        文本文件:用记事本打开,人能看懂的就是文本文件
    字节流:所有的文件都能够读取和写入

2、数据流向分类
    输入流:用来从文件中【读】取数据
    输出流:用来把数据【写】到文件中去

二、FileWriter类

是用来往文件中写数据的便捷类
步骤:
    1)创建FileWriter对象,关联文件
        FileWriter fw=new FileWriter("D:\\a.txt");
    2)调用write方法,往文件中写数据
        fw.write("HelloJava");
    3)调用flush刷新数据
        fw.flush();
    4)调用close()释放资源
        fw.close();

常用的方法
    write(String str)
        往文件中写字符串
    write(String str,int index,int count)
        把字符串的一部分,写到文件中去
    write(char[] chs)
        往文件中写一个char的数组,把数组中的元素以字符串的方法写到文件中
    write(char[] chs,int index,int count)
        把数组的一部分写到文件中去
    wirte(int ch)
        往文件中写一个字符,可以是'a'    97

三、FileReader类

是用来读取文件中数据的便捷类
步骤:
    1)创建FileReader对象,关联一个文件
        FileReader fr=new FileReader("D:\\a.txt");

    2)使用read方法读取文件
        //一次读一个字符
        /*int ch=0;
        while((ch=fr.read())!=-1){
            System.out.print((char)ch);
        }*/

        //一次读一个字符数组
        char[] chs=new char[1024];
        int len;
        while((len=fr.read(chs))!=-1){
            //每次读取的字符,存储到chs数组中的
            String str=new String(chs,0,len);
            System.out.print(str);
        }

    3)释放资源
        fr.close();

四、复制文本文件

1.一次复制一个字符
    //源文件,用来读的,使用FileReader
    FileReader fr=new FileReader("a.txt");
    //目标文件,用来写的,使用FileWriter
    FileWriter fw=new FileWriter("copy.txt");

    //一边读,一边写
    int ch=0;//记录的是每次读取到的字符,对应的整数值
    while((ch=fr.read())!=-1){
        fw.write(ch);
    }
    //释放资源
    fr.close();
    fw.close();

2.一次复制一个字符数组
    //源文件,用来读的,使用FileReader
    FileReader fr=new FileReader("a.txt");
    //目标文件,用来写的,使用FileWriter
    FileWriter fw=new FileWriter("copy.txt");

    //准备一个数组
    char[] chs=new char[1024];
    int len=0;//记录每次读取的有效字符个数
    while((len=fr.read(chs))!=-1){
        //String str=new String(chs,0,len);
        fw.write(chs,0,len);
    }
    //释放资源
    fr.close();
    fw.close();

五、字符缓冲流

BufferedReader  读取文件
    String readLine()   一次读取一行,如果读到文件末尾返回null
BufferedWriter  写入文件
    newLine() 写入一个换行符(具有跨平台性)

public class BufferedDemo{
    public static void main(String[] args){
        //需求:把a.txt文件中的内容,复制到copy.txt文件中去
        //源文件   a.txt
        BufferedReader br=new BufferedReader(new FileReader("a.txt"));
        //目标文件  copy.txt
        BufferedWriter bw=new BufferedWriter(new FileWriter("copy.txt"));

        //每次读取一行
        String line;
        while((line=br.readLine())!=null){
            //一次写一行
            bw.write(line);
            bw.newLine();//写换行
        }
        //释放资源
        br.close();
        bw.close();
    }
}


五、练习题

1.创建一个Student类,属性(学号,姓名,年龄,分数)
2.创建5个Student对象,并且给属性赋值,存储到ArrayList集合中
3.把集合中的学生信息,存储到student.txt文件中,格式如下:
    heima001    张三  18  100
    heima002    李四  20  99
    heima003    王五  16  98

4.把student.txt文件中的学生信息,读取出来,存入ArrayList集合
5.遍历ArrayList集合

public class Test{
    public static void main(String[] args){

        //创建5个Student对象,并且给属性赋值,存储到ArrayList集合中
        ArrayList<Student> list=new ArrayList<>();
        list.add(new Student("heima001", "张三", "30", "100"));
        list.add(new Student("heima002", "李四", "18", "76"));
        list.add(new Student("heima003", "王五", "20", "89"));
        list.add(new Student("heima004", "赵六", "40", "99"));
        list.add(new Student("heima005", "田七", "11", "91"));

        //把集合中的学生信息,存储到student.txt文件中,格式如下:
        //为了往student.txt文件中写数据,需要使用BufferedWriter
        BufferedWriter bw=new BufferedWriter(new FileWriter("student.txt"));

        //遍历集合获取到每一个学生对象
        for (int i = 0; i <list.size(); i++) {
            Student stu=list.get(i);
            bw.write(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getScore());
            bw.newLine();
            bw.flush();
        }
        //释放资源
        bw.close();

        //把student.txt文件中的学生信息,读取出来,存入ArrayList集合
        ArrayList<String> list2=new ArrayList<>();
        //读取诗句,使用BufferedReader
        BufferedReader br=new BufferedReader(new FileReader("student.txt"));
        String line=null;//记录每一行数据
        while((line=br.readLine())!=null){
            list2.add(line);
        }

        //遍历ArrayList集合
        for (int i = 0; i < list2.size(); i++) {
            System.out.println(list2.get(i));
        }
    }

}

猜你喜欢

转载自blog.csdn.net/jeremy_ke/article/details/81227698