String

字符输入流:FileReader

字符输出流:FileWriter

FileWriter使用步骤:创、写、关

 

注意:如果指定的文件不存在,会创建文件,如果已经存在,会覆盖写入。

若不希望覆盖写入,希望追加写入,那么用另一种重载形式的构造方法

 public FileWriter(String fileName, boolean append)第二个参数如果是true值,则会追加写入。

几种成员方法:

BufferedWriter也是输入字符输出流(常用)。

构造方法:

public BufferedWriter(FileWriter fw),参数就是一个普通的FileWriter对象

区别:1.BufferedWriter有一个长度为8192的char[] 字符数组,当缓冲区用。

           2.有一个额外的换行方法

public void newLine(),将会自动根据系统不同,选择写入\r\或者是\r或者是\n。

 

 

 FileReader使用步骤:创、读、关

 public int read()方法每次只会读取一个,每次调用read方法,都会尝试读取下一个字符.

小技巧:如果一个int变量确实是一个文字对应的ASCII值,那么可以通过格式将其翻译成文字:

(char) 变量名称

数据被读取完,继续使用read()方法,就会返回-1值。

为了提高效率,FileWriter提供了另一种重载形式的方法:

public int read(char[] buf),一次读取整个字符数组的数据

参数是一个字符数组,用来承载读取到的多个字符。

返回值代表的是数据当中读取到的有效个数。

若读取文本中的数据完毕,继续读取,返回值int的值为-1。

 

读取文本中的"hello",数据长度为2,hello长度为5,读三次,第二次的一个" l ",没有被覆盖,第三次会读出 " ol "

 

BufferedReader也是字符输入流(常用)

 使用和FileReader基本一样,

public BufferedReader(FileReader fr)参数是普通的FileReader对象

 BufferedReader提供了额外的一个方法,可以读取一行的字符串

public String readLine()读取一整行字符串,返回值当中不包含换行符.

 1.练习:读出来的数据存入集合中

        ArrayList<String> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("file01.txt"));
        String str;
        while ((str = br.readLine()) != null) {
            list.add(str);

        }
        br.close();
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

2.读出来的数据存入集合中,倒序在写入文本文件中

        BufferedReader br = new BufferedReader(new FileReader("练习2.txt"));
        ArrayList<String> list = new ArrayList<>();

        String str;

        while ((str = br.readLine()) != null) {
            list.add(str);
        }
        br.close();
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter("练习2.txt"));

        for (int a = list.size()-1; a > -1; a--) {
            String st = list.get(a);
            bw.write(st);
            bw.newLine();
        }
        bw.close();

方法参数可以是什么类型?(都可以)
1.基本类型
2.数组
3.字符串
4.自定义的类

        method(50);
        System.out.println("========================");

        int[] arr = { 15, 12, 12};
        method1(arr);
        System.out.println("========================");

        String str = "How do you do ?";
        method2(str);
        System.out.println("========================");
        Student stu = new Student("小丹丹",18);
        method3(stu);
    }
    // 使用基本类型作为方法的参数,可以
    public static void method(int i) {
        i+= 20;
        System.out.println(i);
    }
    // 使用数组作为方法的参数,也可以
    public static void method1(int[] arr) {
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
    }
    // 使用字符串作为方法的参数,也可以
    public static void method2(String str) {
        String s= str.replace("o", "*");
        System.out.println(s);
    }
    // 使用自定义的类作为方法的参数,照样可以!
    public static void method3(Student stu) {
        System.out.println("姓名: " + stu.getName() + ",年龄: " + stu.getAge());
    }
}

Stuent类:

    private String name;
    private int age;

    public Student(){

    }
    public Student(String name, int age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

方法的返回值可以是什么类型?(都可以)
1. 基本类型
2. 数据
3. 字符串
4. 自定义的类

        int a = method1();
        System.out.println(a);
        System.out.println("======================");

        int[ ] arr = method2();
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
        System.out.println("=================");


        String str = method3();
        System.out.println(str);
        System.out.println("=================");

        Student stu = method4();
        System.out.println("姓名: " + stu.getName() + ",年龄:" + stu.getAge());
    }
    // 使用基本类型作为方法的返回值,可以
    public static int method1() {
        int a = 10;
        return a;
    }
    // 使用数组类型作为方法返回值,也可以
    public static int[] method2() {
        int[] arr = {10, 20, 30};
        return arr;
    }
    // 使用字符串作为方法返回值,也可以
    public static String method3() {
        String str = "丹丹";
        return  str;
    }
    // 使用自定义的类作为方法的返回值,照样可以!
    public static  Student method4() {
        Student stu = new Student("冯一丹", 19);
        return stu;


    }

知识总结

1. IO流是用来传输数据的技术、典型的应用包括读写文件

2. IO流的分类:

        a) 字节输入流

        b) 字节输出流

        c) 字符输入流:FileReader  BufferedReader

        d) 字符输出流:FileWriter    BufferedWriter

3. FIleWriter 一般使用步骤: 创、写、关。

        a)创:

FileWriter fw = new FileWriter("file01.txt");

        b)  写:

fw.writer("Hello");

        c)关:

fw.close();

4. FileWriter 如何才能追加写入呢? 构造方法第二个参数为 true即可

5. 如何实现换行?

        a)Windows当中: \r\n

        b) macOs 当中: \r或者\n

        c) Linux 当中: \n

6. 五种writer方法的重载:

        a)写字符串的整体

        b)写字符串的部分

        c)写字符数组的整体

        d)写字符数组的部分

        e)写单个字符对应的数字(参考ASCII码表或者Unicode表)

7.FileReader一般使用步骤:创、读、关。

        a)创:

FileReader fr = new FileReader("file01.txt")

        b)读单个字符
           

               int ch;

               while ((ch = fr.read() != -1) {

                        //....
    }

        c)读字符数组:

char[] buf = new char[1000];

        int len; // 有效个数

        while ((len = fr.read(buf)) ! = -1) {

                String str = new String (buf, 0, len);        

    }

 8. BufferedXxx 和 FileXxx 有什么区别?BufferedXxx有8192长度的char[]字符数组作为缓冲区,所以性能更加高一些

9.BufferedWriter有额外的方法: public viod newLine();根据操作系统自动选择写入换行符

10.BufferedReader有额外的方法:public String  readLine()读一行字符串,不包含换行符。

11.关于方法的参数和返回值类型:

        a)任何数据类型(基本类型、数组、字符串、自定义的类)都可以作为参数

        b)任何数据类型(基本类型、数组、字符串、自定义的类)都可以作为返回值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值