package com.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
public class Stream {
 /**
  * 尽管RandomAccessFile可以进行读写,毕竟操作起来麻烦。所以字节流和字符流是重点。
  * 1个字符等于2个字节(Byte),一个字节等于8位(bit)
  * 字节流:输入流(InputStream)和输出流(OutPutStream)
  * 字符流:输入流(Reader)和输出流(Writer)
  * ----------------------------------------------------
  * 步骤:1:File找文件
  *      2:使用字节流或字符流的子类为InPutStream,OutPutStream,Reader,Writer实例化
  *      3:读写操作
  *      4:关闭
  * -----------------------------------------
  * 一:字节输出流OutPutStream,此类是一个抽象类,所以使用时候需要子类进行实例化操作。
  * public abstract class OutputStreamextends Objectimplements Closeable, Flushable
  * 子类:FileOutPutStream,向文件中写内容
  * OutPutStream:方法  写入全部字节数组;写入部分字节数组
  * public void write(byte[] b) throws IOException
       public void write(byte[] b,
                  int off,
                  int len)
           throws IOException将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。write(b, off, len) 的常规协定是:将数组 b 中的某些字节按顺序写入输出流;元素 b[off] 是此操作写入的第一个字节,b[off+len-1] 是此操作写入的最后一个字节。
        OutputStream 的 write 方法对每个要写出的字节调用一个参数的 write 方法。建议子类重写此方法并提供更有效的实现。 
        如果 b 为 null,则抛出 NullPointerException。 
        如果 off 为负,或 len 为负,或者 off+len 大于数组 b 的长度,则抛出 IndexOutOfBoundsException。 
  * @throws Exception 
       ---------------------------------------------------------------
       
  * 字符输出Writer,输入Reader,它们也都属于抽象类
  * 如果要进行文件的保存用子类FileWriter,FileReader
  * public void write(String str) throws IOException
  * --------------------------------------------------
  * 字符流和字节流区别:
  * 字节流直接与文件关联,不使用缓冲区
  * 字节流通过缓冲区与文件关联。
       一般来讲:硬盘上保存是以字节形式,所以流的操作字节流用的多。但是文件的读写字符流用的多。
  *
  *             
  * 
  */
 public static void main(String[] args) throws Exception {
  File file = new File("E:"+File.separator+"hello.txt");
 /***********************************写入Outputstream***************/
  //OutputStream os = new FileOutputStream(file,false);//实例化 ,true表示追加,false表示覆盖
        //String str = "hello,my name is juwn";
       // byte[] b = str.getBytes(); //字符串转为byte数组
       // os.write(b); //方法一
        //for(int i=0;i<b.length;i++){
        // os.write(b[i]); //方法二 一条一条的写  该方法常用
        //}
       // os.close();
        //写入的所有新内容将覆盖了文件中原来的内容,如果追加内容,就要看FileOutputStream的构造方法
        //public FileOutPutStream(File f,boolean append){} //如果将append设为true表示追加
     /***********************************读取inPutstream***************/
        InputStream is = new FileInputStream(file);//实例化
        //1:public abstract int read()throws IOException 每次读一个字节
        //2:public int read(byte[] b) throws IOException 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中
       /***************第一种读取方法*****************/
       // byte[] b = new byte[1024]; //字节数组用来保存读取的数据流
        //is.read(b);
        //System.out.println(new String(b));
        /*****************第二种读取方法*************************/
        int len = (int) file.length();
        byte[] b = new byte[len];
        for(int i=0;i<b.length;i++){
         b[i] = (byte) is.read(); //一个一个读取 常用
        }
        System.out.println(new String(b));
        is.close();
       
       /**-----------------------------------------------*/
       // File file = new File("E:"+File.separator+"hello.txt");
  //写入
  //Writer w = new FileWriter(file,true);//追加写入
  //w.write("     hello,my name is juwm");
  //w.close();
  //读取
  //Reader r = new FileReader(file);
  //char[] c = new char[(int) file.length()];
  //r.read(c);
  //System.out.println(new String(c));
  //for(int i=0;i<c.length;i++){
  // c[i] = (char) r.read();
  //}
  //System.out.println(new String(c));
  //r.close();
 }
}