##IO流基础知识掌握(三)总结

IO流(File)

IO流分为输入流和输出流,输入流就是吧数据从其他设备中读取到内存中,可以简称一个的动作;输出流就是吧数据从内存中写到其他设备中,可以简称的动作

而我们平时可以吧输入流和输出流又分为字符流和字节流:

下面我们来回顾下字节流中的输入流和输出流:

1,字节流:

  一。 java.io.InputStream   是所有字节流输入流的超类

  1.FileInputStream extends InputStream  2.FileInputStream 文件字节输入流  3.作用:把硬盘上的数据 读取到内存里

  既然是输入流,那么它一定是读的过程,它的方法有哪些呢?:

  read();   读取下一个字节

  read ( byte[] bytes);  可以通过字节数组提升读取的效率

  close(); 闭流

  如何用:

  FileInputStream(String name);  String name 是路径

  FileInputStream(File name);   

  字节输入流的使用步骤(重点):

    1.创建FileInputStream对象  构造方法中要绑定读取的数据源

    FileInputStream fis = new FileInputStream("d:\ruirui.txt");

    2.使用对象read()读取文件

      2.1普通方法

 /*
        * 读取是一个重复过程  用循环   fis.read(); 读不到返回-1
        * */
        int len = 0;
        while((len=fis.read())!=-1){
            System.out.println(len);
        }

      2.2利用字节数组方法

     byte[] bytes= new byte[1024];
        int len = 0;
        while((len=fis.read(bytes))!=-1){
            //字节数组转字符串     new  String(byte[] bytes,int off,int length)
            //    bytes={97,98,99}   new String(bytes,0,bytes.length)--->abc
            System.out.println(new String(bytes,0,len));

在字节数组中,我们如果想要把字节数组转换成字符串,可以用new String(数组名,0,len)

字节输入流 一次读取多个字节:

  read(byte[] bytes);

  byte[]参数的作用  起到缓冲作用,读取的是多个字节,数组长度1024(1kb)

    3.释放资源  

    fis.close();

  二。java.io.OutpuStream  是所有字节输出流的超类

  1.FileOutputStream extends OutputStream  2.FileOutnputStream 文件字节输入流  3.作用:把内存中的数据存写到硬盘上

  既然是输出流,那么它一定是写的过程,它的方法有哪些呢?:

  write();   写

  write ( byte[] bytes);  可以通过字节数组提升写的效率

  close(); 闭流

  如何用:  

  字节输出流的使用步骤(重点):

    1.创建FileOutnputStream对象  构造方法中要绑定读取的数据源

    FileOutnputStream fos = new FileoutputStream("d:\ruirui.txt");

    2.使用对象write()写文件

    fos.write(b);

    3.释放资源  

    fos.close();

  三。学完字节的输入流和输出流,那么我们可不可以综合练习下,如果取复制一个文件呢?

public class Demo03Copy {
    public static void main(String[] args) throws IOException{
        long s = System.currentTimeMillis();
        //1  创建输入流对象   读的数据源
        FileInputStream fis  = new FileInputStream("d:\kgc.jpg");
        //2   创建输出流对象  写入的目的
        FileOutputStream fos = new FileOutputStream("D:\my_java\kgc.jpg");
        //3  使用字节输入流中  read 读取文件
//        int len  =0;
//        while((len=fis.read())!=-1){
        //4   使用字节输出流  write  写到目的文件中
//            fos.write(len);
//        }
           byte[] bytes=new byte[1024];
           int len=0;
           while((len=fis.read(bytes))!=-1){
               fos.write(bytes,0,len);
           }
//5 释放资源 先开的后关 long ss = System.currentTimeMillis(); fos.close(); fis.close(); System.out.println("复制图片用了"+(ss-s)+"毫秒"); } }

2,字符流

  2.1 java.io.Reader 字符的输入流 顶层父类     FileReader  extends InputStreamReader extends Reader  FileReader 称为文件字符输入流

它的方法都有哪些呢?:  GBK 一个汉字 两个字节

  read();

  close();

步骤:

  1,使用FileReader对象  构造方法当中绑定读取的数据的数据源

     FileReader fr = new FileReader("d:\bobo.txt");

  2,使用该对象中的方法读取数据read();

     int len = 0;
        while((len=fr.read())!=-1){
            System.out.println((char)len);
        }

  3,释放资源

    fr.close();

  2.2 java.io.Writer :字符输出流 抽象类  FileWriter extends OutputStreamWriter extends Writer  将内存中的数据 写入到文件中

    那么字符输出流的方法都有哪些呢?

    write();    flush();    close();
    步骤:

      1,创建FileWrite对象   数据的目的地

    FileWriter fw  = new FileWriter("d:\d.txt");

      2,用对象中的方法Write();

     fw.write(97);
        fw.write("我爱java");

      3,flush();  讲缓冲区当中的数据写到文件中

     fw.flush();

      4,释放资源

      fw.close();

字符流写数据的其他方法:

public class Demo03Write {
    public static void main(String[] args) throws IOException{
        FileWriter fw  =new FileWriter("d:\dd.txt");
        char[] cs = {'a','b','c','d','e'};
        fw.write(cs);
        fw.write(cs,1,3);
        fw.write("小蚂蚁");
        fw.write("动物园的世界",2,3);//   最后一个参数   代表输出的个数
        fw.close();
    }
}

3,在字符输出流中多出现了flush方法,那么flush和close有什么区别呢:

  3.1flush()刷新 流对象可以继续使用

  3.2close()刷新缓冲区,然后通过释放资源的方式,流不能再使用

  3.3UTF-8是一个汉子三个字节

4,Properties      重点

4.1  java.util.Properties 集合 extends Hashtable<k,v>implements Map<k,v>
4.2  持久的属性集 propertis可以保存在流中,或者从流中加载
4.3  唯一的一个和IO流相结合的结合
4.4  stroe把六中临时的数据,持久化到硬盘中存储
4.5  load把硬盘中的文件(键值对读取到集合中使用)唯一的方法

在使用过程中我们一般分为这几步:
  1,创建properties集合对象
  2,利用load方法读取数据,并保存到对应的集合中(写到这一步,大家是不是感觉和我们之前的map集合很像)
  3,便利集合,通过key值取确定它的value值
下面我们来看下实例:
public class Demo01Properties {
    public static void main(String[] args) throws IOException{
        show03();
    }
    // 3 使用Properties集合中的方法  load  把硬盘上的文件(键值对)  读取到集合中使用
    private static void show03()throws IOException{
        // 1 创建集合
        Properties po = new Properties();
        // 2 load方法读取数据  并保存到对应的集合中
        po.load(new FileReader("d:\kgc11.txt"));
        //3  遍历集合po
        Set<String> s = po.stringPropertyNames();
        for(String key:s){
            String value = po.getProperty(key);
            System.out.println(key+"="+value);
        }

    }
    //2 把集合中的临时数据写到硬盘上  store 把流中临时的数据  持久化到硬盘中存储
      //  load把硬盘中的文件(键值对) 读取到 集合中使用
    private static void show02()throws IOException{
        Properties po = new Properties();
        po.setProperty("PWD","dou6666");
        po.setProperty("user","ruirui");
        po.setProperty("周慧敏","168");
        po.setProperty("古丽娜扎","160");
        //1 创建字节输出流 //字符输出流    构造方法中要绑定输出的目的地
//        FileWriter fw = new FileWriter("d:\kgc1.txt");
//        po.store(fw,"save data");
//        fw.close();
        po.store(new FileWriter("d:\kgc11.txt"),"");
    }

    //1  使用properties 集合存储数据  遍历取出
    /*propertes 集合有一些操作字符串的方法
    setProperties(String key,Strign value)
    * getProperties(String key);
    * stringPropertyNames();----->keySet方法
    * */
    private static void show01(){
        //1 存值
        Properties po = new Properties();
        po.setProperty("赵丽颖","168");
        po.setProperty("迪丽热巴","165");
        po.setProperty("周慧敏","168");
        po.setProperty("古丽娜扎","160");
        //  2 取值
        Set<String> set = po.stringPropertyNames();
        for(String key:set){
            String value = po.getProperty(key);
            System.out.println(key+"=  "+value);
        }
    }
}

5,缓冲流

缓冲区也分为字节输入缓冲流和字节输出缓冲流,字符输入缓冲流和字符输出缓冲流

5.1 字节缓冲输出流 BufferedOutputStream  extends  OutputStream

它的方法都有哪些呢?

    close();  write();  flush();

使用步骤:

  1,创建FileOutputStream对象

  2,创建BufferedOutputStreamz对象

  3,使用BufferedOutputStream中的方  write();  写到缓冲区

  4,使用BufferedOutputStream中的方法  flush();  

  5,释放

下面我们来看看例子:(做参考)
public class Demo01BufferedOutputStream {
    public static void main(String[] args)throws IOException {
        //1 创建FileOutputStream 对象
        FileOutputStream fos = new FileOutputStream("d:\ruirui.txt");
        //2 创建 BufferedOutputStream对象
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //3 使用 BufferedOutputStream中的方法   write();  写到缓冲区
        bos.write("我把数据写入到内部缓冲区".getBytes());
        //4 使用 BufferedOutputStream中的方法   flush();
        bos.flush();
        //5 释放
        bos.close();
    }
}

5.2 字节缓冲输入流 BufferedInputStream  extends  InputStream

它的方法都有哪些呢?

    close();  read();

使用步骤:

  1,创建FileInputStream对象

  2,创建BufferedInputStreamz对象

  3,调用read();   

  4,释放

下面我们来看看例子:(做参考)
public class Demo02BufferedInputStream {
    public static void main(String[] args) throws IOException{
        //1  创建FileInputStream对象
        FileInputStream fis = new FileInputStream("d:\ruirui.txt");
       // 2   创建BufferedInputStream对象
        BufferedInputStream bis = new BufferedInputStream(fis);

       // 3 调用read();
        byte[] bytes = new byte[1024];
        int len= 0;
        while((len=bis.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
        }
        //4 释放资源
        bis.close();
    }
}

今天我们还有一道练习题:希望大家都可以练习下,巩固下我们所学的知识点:

利用缓冲区把一个文件复制到另一个地方:

public class Demo01Write {
    public static void main(String[] args) throws IOException{
        show01();
    }
    private static void show01()throws IOException{
        //1,新建FileInputStream对象
        FileInputStream fis = new FileInputStream("d:\ASCII.jpg");
        //2,新建FileOutputStream对象
        FileOutputStream fos = new FileOutputStream("C:\AppData\a.jpg");
        //3, 创建BufferedInputStream对象
        BufferedInputStream bis = new BufferedInputStream(fis);
        //4,创建BufferedOutputStream对象
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //5,利用缓冲流里面的write和read方法进行复制
        byte[] bytes=new byte[1024];
        int len=0;
        while((len=bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }
        //6, 释放
        bos.flush();
        bos.close();
        fos.close();
        bis.close();
        fis.close();
    }
}
原文地址:https://www.cnblogs.com/liurui-bk517/p/10927357.html