Java-I/O流知识点

**

第一节. IO流

**
一、 流的作用和原理
*** 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
二、流的分类
<一> 、字节流
1、字节输入流。
文件输入流FileInputStream:

  
//创建字节文件输入流
        FileInputStream fis = new FileInputStream("gp4.txt");
        //读取
        byte[] buf = new byte[1024];
        int len = -1;
        while ((len = fis.read(buf)) != -1) {
            for (int i = 0; i <len  ; i++) {
                System.out.print((char) buf[i]);
            }
        }
        //关闭流
        fis.close();
    }}

2、字节输出流
文件输入流FileInputStream

 // 创建字节输出流
       FileOutputStream fio=new FileOutputStream("gp4.txt");

        // 写入
        fio.write(1000);
        fio.write(100);
        //  把字符串转成数组
               String s="this is a dood idea!";
               byte[] buf=s.getBytes();
                 fio.write(buf, 0, buf.length);

         // 关闭流
          fio.close();
          System.out.println("写入成功!");

    }

<二>、字符流
字符输入流:

//使用字符输入流读取文件
      FileReader fir=new FileReader("gp4.txt");

      char[] ch=new char[1024];
      int len=-1;
          while ((len=fir.read(ch))!=-1){

              String data=new String(ch, 0, len);// 把字符数组转换为字符串。
              System.out.println(data);

          }

        //关闭流
        fir.close();

字符输出流:

//使用字符流写入文件
        //创建字符输出流
        FileWriter fis=new FileWriter("d:\\gp4.txt");
        // 写入
        for (int i = 0; i < 10; i++) {
            fis.write("一屋不扫,何以扫天下!\r\n");
            fis.flush();
        }
        //关闭流
        fis.close();
        System.out.println("写入成功!");

<三>转换流
解决乱码问题。

public class InputStreamReaderDe {
    public static void main(String[] args) throws  Exception{
        FileInputStream fio=new FileInputStream("gp4.txt");

        //创建转换流
        InputStreamReader fis=new InputStreamReader(fio, "GBK");

        //读取
        char[] buf=new char[1024];
        int len=-1;
      /*  if((len=fis.read(buf))!=-1){

            String str=new String(buf,0,len);
            System.out.println(str);


        }*/
      while ((len=fis.read(buf))!=-1){
          String str=new String(buf,0,len);
          System.out.println(str);
      }
    }
}
public class OutputStreamWriteDe {
    public static void main(String[] args) throws  Exception{
        FileOutputStream fis=new FileOutputStream("d:\\lod.txt");
        //创建转换输出流
        OutputStreamWriter  fos=new OutputStreamWriter(fis, "GBK");

        // 写入
        for (int i = 0; i <10 ; i++) {
            fos.write("好好学习,天天向上!\r\n");
            fos.flush();
        }
        //关闭
        fos.close();
        System.out.println("写入成功!");
    }
}

<四> 缓冲流
作用:主要是为了增强基础流的功能而存在的,提高了流的工作效率【读写效率】

注意:如果使用记事本创建的文件,文件是utf-8或者unicode编码,文件的前面有一个BOM(Byte Order Mark)头,BOM作用指定文件使用的编码类型。GBK编码没有添加bom头。
utf-8:EF BB BF
unicode 小端: FF FE 66 00
unicode 大端 :FE FF 00 66

//  字节输入缓冲流
public class Demo01 {
    public static void main(String[] args)throws  Exception{
        // 创建缓冲流
        BufferedInputStream   fis=new BufferedInputStream(new FileInputStream("gp4.txt"));
        // 读取
        byte[] buf=new byte[1024];
        int len=-1;
        while ((len=fis.read(buf))!=-1){

              String s=new String(buf, 0,len);
            System.out.println(s);
        }

        // 关闭流
        fis.close();

    }
}
// 创建字节缓存输出流
public class Demo02 {
    public static void main(String[] args)throws  Exception {
          //创建流

        FileOutputStream fio=new FileOutputStream("out.txt");
        BufferedOutputStream fis=new BufferedOutputStream(fio);

        // 写入

        for (int i = 0; i < 10; i++) {
            fis.write("好好学习,天天向上!\r\n".getBytes());

            fis.flush();

        }
        //关闭流
        fis.close();
        System.out.println("写入成功!");

    }
}

字符缓冲流:

//  使用字符缓冲流读取数据
public class Demo04 {
    public static void main(String[] args) throws Exception{
        //创建对象

        BufferedReader  fis=new BufferedReader(new FileReader("gp4.txt"));

        //读取
            /*// 第一种读取
          char[] buf=new char[1024];
        int len=-1;
        while((len=fis.read(buf))!=-1){

            String s=new String(buf, 0,len);
            System.out.println(s);
        }*/
        //第二种读取   读取一行
        String line=null;
        while ((line=fis.readLine())!=null){
            System.out.println(line);

        }

        //关闭流
        fis.close();
    }
}

//  使用字符缓冲输出流写入
public class Demo05 {
    public static void main(String[] args)throws Exception {
        //创建对象
        BufferedWriter   fis=new BufferedWriter(new FileWriter("out2.txt"));

        //写入

        for (int i = 0; i <10 ; i++) {
            fis.write("今天是个好日子!");
            fis.flush();
            fis.newLine();  // 换行
        }

        //关闭流
        fis.close();
        System.out.println("写入成功!");


    }
}

使用缓冲流实现复制:


//使用缓冲流实现复制
public class Demo03 {
     public static void main(String[] args)throws  Exception {
         //  创建流
         BufferedInputStream  fio=new BufferedInputStream(new FileInputStream("out.txt"));
         BufferedOutputStream fis=new BufferedOutputStream(new FileOutputStream("out1.txt"));

         // 读写
         byte[] buf=new byte[1024];
         int len =-1;
         while ((len=fio.read(buf))!=-1){
             fis.write(buf, 0, len);
             fis.flush();

         }

         //关闭流
         fis.close();
         fio.close();

         System.out.println("复制成功!");

     }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值