java 缓冲字符流 BufferedReader BufferedWriter

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class Test2 {
     
    public static void main(String[] args) {
     
        try {
     
            Test2.testBufferedReader();
            Test2.testBufferWriter();
        } catch (Exception e) {
     
            e.printStackTrace();
        }

    }
    public static void testBufferedReader()throws Exception{
     
        FileReader r=new FileReader("E:\\IDEAproject\\src\\b\\tt.txt");
        BufferedReader br=new BufferedReader(r);
        char c[]=new char[100];
        int len=0;
        while((len=br.read(c))!=-1){
     
            System.out.println(new String(c,0,len));
        }
        br.close();
        r.close();
    }
    public static void testBufferWriter()throws Exception{
     
        FileWriter w=new FileWriter("E:\\IDEAproject\\src\\b\\tt3.txt");
        BufferedWriter bw=new BufferedWriter(w);
        String s="kiana time";
        bw.write(s);
        bw.flush();
        bw.close();
        w.close();
    }
//缓冲字符流复制文件
    public static void copyFile()throws Exception{
     
        BufferedReader br=new BufferedReader(new FileReader(""));
        BufferedWriter wr=new BufferedWriter(new FileWriter(""));
        char s[]=new char[100];
        int len=0;
        while ((len=br.read(s))!=-1){
     
            wr.write(s,0,len);
        }
        wr.flush();
        wr.close();
        br.close();

    }
}

运行结果:
糖醋酱板鸭
盐巴秘制飞鱼丸
油炸草履虫
冰激凌犹大德莉傻

你可能感兴趣的:(java入门编程)