package IO字符流;

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

public class CharStreamBufferDemo1 {

    public static void main(String[] args) throws IOException {
        method_1();
        method_2();
    }
    public static void method_2() throws IOException {
        FileReader fr1 = new FileReader("IOTest.txt");
        BufferedReader br1 = new BufferedReader(fr1);
        String line;
        //缓冲区的read()方法是重写的,不是在硬盘中读取数据,而是在内存中读取数组,所以效率更高
        //readline();原理:调用缓冲区的read()方法,然后进行判断换行标记,将标记前的数据变成字符串返回;
        while((line=br1.readLine())!=null){
            System.out.println(line);
        }
    }
    public static void method_1()throws IOException{
        FileWriter fw1 = new FileWriter("IOTest.txt");
        BufferedWriter bw1 = new BufferedWriter(fw1);
        bw1.write("abc");
        bw1.flush();
        bw1.newLine();
        bw1.write("IO");
        bw1.close();
    }

}
package IO字符流;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class LineNumberReaderDemo {

    public static void main(String[] args) throws IOException {
        FileReader fr1 = new FileReader("demo.txt");
        LineNumberReader lnr1 = new LineNumberReader(fr1);
        String line;
        //默认从0开始;
        lnr1.setLineNumber(0);
        while((line=lnr1.readLine())!=null){
            //读取限定行内容时可以使用;
            if(lnr1.getLineNumber()>2)
               System.out.println(line+":"+lnr1.getLineNumber());
        }

    }

}
package IO字符流;

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

/**自定义一个缓冲区,模拟BufferedReader里面的read readLine close 等方法
 * 分析:
 * 缓冲区中无非就是封装一个数组,并对外提供更多的方法对数组进行访问,
 * 其实这些方法最终操作的都是数组的角标。
 * 
 * 缓冲的原理:
 * 其实就是从源中获取一批数据,装进缓冲区中,
 * 从缓冲区中不断取出一个一个数据;
 * 
 * 在此次取完后,再从源中继续取一批数据进缓冲区,
 * 当源中的数据取光时,用-1作为结束标记。
 * 
 * */
public class MyBufferedReaderTest {
    private FileReader  fr;
    //创建一个数组作为缓冲区;
    private char [] buf = new char[1024];
    //定义一个指针操作数组元素,当操作最后一个元素后指针归零;
    private int pos=0;
    //顶一个变量用于记录缓冲区的数据个数,当该数据减到零,就从源中继续获取数据到缓冲区中。
    private int count=0;
    public MyBufferedReaderTest(FileReader  fr) {
        this.fr=fr;
    }
    public int myRead() throws IOException{
        //1、从源中获取一批数据到缓冲区中。需要判断,只有计数器为0时,才需要取数据。
        if(count==0){
           count=fr.read(buf);
           //每次获取数据到缓冲区后角标归零;
           pos=0;
          
        }
        if(count<0){
            return -1;
        }
         char ch= buf[pos];
           pos++;
           count--;
           return ch;
    }
    public String myReadLine() throws IOException{
        StringBuilder sb = new StringBuilder();
        int ch=0;
        while((ch=myRead())!=-1){
            if(ch=='\r')
                continue;
            if(ch=='\n'){
                return sb.toString();
            }
            //将从缓冲区中读到的字符,存到另一个缓冲区中
            sb.append(buf[ch]);
        }
        if(sb.length()!=0){
            return sb.toString();
        }
        //读到-1,返回空,表明没有数据可读了;
        return null;
        
    }
    public void myClose() throws IOException{
        fr.close();
    }

    
}
package IO字符流;

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

public class BufferedTest {

    public static void main(String[] args) throws IOException {
        FileReader fr1 = new FileReader("buff.txt");
        BufferedReader br1 = new BufferedReader(fr1);
        FileWriter fw1 = new FileWriter("copy.txt");
        BufferedWriter bw1 = new BufferedWriter(fw1);
        String line = null;
        int count=0;
        while ((line = br1.readLine()) != null) {
            if (line.contains("java")){
                bw1.write(line);
                bw1.newLine();
                bw1.flush();
                count++;
            }
            
        }
        System.out.println("发现匹配文件"+count+"个");
        /*
         * int ch=0; while((ch=br1.read())!=-1){ bw1.write(ch); }
         */
        br1.close();
        bw1.close();

    }

}
package IO字节流;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Input_outputStreamDemo {

    public static void main(String[] args) throws IOException {

        demo_write();
        demo_read();
    }

    public static void demo_read() throws IOException {
        //1、创建一个读取流对象。和指定文件关联
        FileInputStream fs1 = new FileInputStream("bytedemo.txt");
        //一次读一个字节
        /*
        int ch=0;
        while((ch=fs1.read())!=-1){
            System.out.println(ch);
        }    
        fs1.close();*/
        //数组读取,建议使用这种
        /*
        byte[]buf = new byte[1024];
        int len=0;
        while((len=fs1.read(buf))!=-1){
            System.out.println(new String(buf,0,len));
        }*/
        //慎用方法,根据文件大小创建数组
        byte[]buf=new byte[fs1.available()];
        fs1.read(buf);
        System.out.println(new String(buf));
    }

    public static void demo_write() throws IOException {
        //1、创建字节流输出对象,用于操作文件
        FileOutputStream fs1 = new FileOutputStream("bytedemo.txt");
        //2、写数据。将字符串转成字节,另外字节是直接写入,不用flush;
        fs1.write("sbcxre".getBytes());
        fs1.close();//关闭资源动作要完成。 缓冲区对象要用到flush;
    }

}
package IO字节流;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyTest {

    public static void main(String[] args) throws IOException {
        copy_demo1();
        copy_demo2(); 
        copy_demo3(); 
        copy_demo4(); 
          
    }

    public static void copy_demo4()throws IOException {
       FileInputStream fi1 = new FileInputStream("bytedemo.txt");
       BufferedInputStream bfi1 = new BufferedInputStream(fi1);
       FileOutputStream fo1 = new FileOutputStream("butedemo.txt");
       BufferedOutputStream bfo1= new BufferedOutputStream(fo1);
       byte[]buf = new byte[fi1.available()];//有局限性,文件过大创建数组都需要很长时间;不建议使用
       bfi1.read(buf);
       bfo1.write(buf);
       bfo1.close();
       bfi1.close();
      
    }

    public static void copy_demo3()throws IOException {
        FileInputStream fi1 = new FileInputStream("bytedemo.txt");
         BufferedInputStream bfi1 = new BufferedInputStream(fi1);
        FileOutputStream fo1 = new FileOutputStream("butedemo.txt");
        BufferedOutputStream bfo1= new BufferedOutputStream(fo1);
        int ch=0;
     while((ch=bfi1.read())!=-1){
              bfo1.write(ch);
              bfo1.flush();//缓冲区必须使用flush ,每个都刷新所以效率很慢;
        }
        bfo1.close();
        bfi1.close();
    }

    public static void copy_demo2() throws IOException {
         FileInputStream fi1 = new FileInputStream("bytedemo.txt");
         BufferedInputStream bfi1 = new BufferedInputStream(fi1);
         FileOutputStream fo1 = new FileOutputStream("butedemo.txt");
         BufferedOutputStream bfo1= new BufferedOutputStream(fo1);
         int len=0;
         byte[]buf = new byte[1024];
         while((len=bfi1.read(buf))!=-1){
               bfo1.write(buf,0,len);
               bfo1.flush();//缓冲区必须使用flush
         }
         bfo1.close();
         bfi1.close();
    }

    public static void copy_demo1() throws IOException {
         FileInputStream fi1 = new FileInputStream("bytedemo.txt");
         FileOutputStream fo1 = new FileOutputStream("butedemo.txt");
         int len=0;
         byte[]buf = new byte[1024];
         while((len=fi1.read(buf))!=-1){
             fo1.write(buf,0,len);
         }
         fi1.close();
         fo1.close();
    }

}