2018-05-09 缓冲流

//字节流的缓冲区 输入类似


//字节流的缓冲区  输入类似
public class BufferedOutputStreamDemo {

    public static void main(String[] args) throws IOException {
        BufferedOutputStream bos=new 
                BufferedOutputStream(new FileOutputStream("E://java//a.txt"));
        bos.write(55);
        byte[] bytes="helloworld".getBytes();
        bos.write(bytes);
        bos.write(bytes,0,3);
        bos.close();

    }

}

//字符流的缓冲区


public class TxtWriter {
    public static void main(String[] args) throws IOException{
                FileWriter fw=new FileWriter("E:\\java\\a.txt");
                BufferedWriter bfw=new BufferedWriter(fw);
                bfw.write("你好".toCharArray()); //字符串编程字符数组
                bfw.flush();
                bfw.newLine();   // 换行符 建议使用  而不是\n\r
                bfw.write("你好".toCharArray()); //字符串编程字符数组
                bfw.flush();
                bfw.write("你好".toCharArray()); //字符串编程字符数组
                bfw.flush();
                bfw.close();
                
        
    }

}
public class TxtWriter {
    public static void main(String[] args) throws IOException{
                
                BufferedReader bfr=new BufferedReader(new FileReader("E:\\java\\a.txt"));
                String len=null;
                while((len=bfr.readLine())!=null) {
                    System.out.println(len);
                }
                /*while((len=bfr.readLine())!=null) {
                    System.out.print(len);
                    当println变为print时输出没有换行,因为readLine()只返回有效字符
                }*/
                bfr.close();
                    
    }

}

缓冲流复制

public class Copy {

    public static void main(String[] args) throws IOException{
        BufferedReader bfr=new BufferedReader(new FileReader("E:\\java\\a.txt"));
        BufferedWriter bfw=new BufferedWriter(new FileWriter("E:\\a.txt"));
        //读一行,写一行,写换行
        String len = null;
        while((len=bfr.readLine())!=null) {
            bfw.write(len);
            bfw.newLine();
            bfw.flush();
        }
        bfw.close();
        bfr.close();
        
    }

}

你可能感兴趣的:(2018-05-09 缓冲流)