Java自学-IO流(4)

Java自学-IO流(4)

1、字符流复制Java文件改进版

Java自学-IO流(4)

Java自学-IO流(4)

package lesson04;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/27 19:15
 * FileName: CopyJavaDemo02
 * Description:
 */
public class CopyJavaDemo02 {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("D:\\Java\\IO\\src\\lesson03\\StringDemo.java");
        FileWriter fw = new FileWriter("Copy.java");

//        int ch;
//        while((ch=fr.read())!=-1){
//            fw.write(ch);
//        }

        char[] chs=new char[1024];
        int len;
        while((len=fr.read(chs))!=-1){
            fw.write(chs,0,len);
        }

        fw.close();
        fr.close();
    }
}

2、字符缓冲流

Java自学-IO流(4)

package lesson05;

import java.io.*;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/27 19:28
 * FileName: BufferedStreamDemo01
 * Description:
 */
public class BufferedStreamDemo01 {
    public static void main(String[] args) throws IOException {
//        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\Java\\IO\\bw.txt"));
//        bw.write("hello\r\n");
//        bw.write("world\r\n");
//        bw.close();

        BufferedReader br = new BufferedReader(new FileReader("D:\\Java\\IO\\bw.txt"));

        //一次读取一个字符数据
//        int ch;
//        while((ch=br.read())!=-1){
//            System.out.print((char) ch);
//        }

        //一次读取一个字节数组的数据
        char[] chs=new char[1024];
        int len;
        while((len=br.read(chs))!=-1){
            System.out.print(new String(chs,0,len));
        }
        br.close();
    }
}

3、字符缓冲流复制Java文件

Java自学-IO流(4)

package lesson05;

import java.io.*;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/27 19:40
 * FileName: CopyJavaDemo01
 * Description:
 */
public class CopyJavaDemo01 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\Java\\IO\\src\\lesson03\\StringDemo.java"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("copy.java"));

        //一次读取一个字符数据
//        int ch;
//        while((ch=br.read())!=-1){
//            bw.write(ch);
//        }

        //一次读取一个字符数组数据
        char[] chs=new char[1024];
        int len;
        while((len=br.read(chs))!=-1){
            bw.write(chs,0,len);
        }
        
        bw.close();
        br.close();
    }
}

4、字符缓冲流特有功能

Java自学-IO流(4)

package lesson05;

import java.io.*;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/27 19:57
 * FileName: BufferedStringDemo02
 * Description:
 */
public class BufferedStringDemo02 {
    public static void main(String[] args) throws IOException {
//        BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
//
//        for (int i = 0; i < 10; i++) {
//            bw.write("hello"+i);
//            bw.newLine();
//            bw.flush();
//        }
//
//        bw.close();

        BufferedReader br = new BufferedReader(new FileReader("bw.txt"));
        String line;
        while((line=br.readLine())!=null){
            System.out.println(line);
        }

        br.close();
    }
}

5、字符缓冲流特有功能复制Java文件

Java自学-IO流(4)

package lesson05;

import java.io.*;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/27 20:11
 * FileName: CopyJavaDemo02
 * Description:
 */
public class CopyJavaDemo02 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("D:\\Java\\IO\\src\\lesson03\\StringDemo.java"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("copy.java"));

        String line;
        while((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        bw.close();
        br.close();
    }
}

6、IO流小结

Java自学-IO流(4)

Java自学-IO流(4)

上一篇:Scanner的使用


下一篇:11.16