美文网首页
【Java】IO流的混乱关系,看得我直呼绝绝子

【Java】IO流的混乱关系,看得我直呼绝绝子

作者: Java架构奶思 | 来源:发表于2021-12-18 19:57 被阅读0次

    一、前言

    这些奇奇怪怪的流在理解起来也是其奇怪怪的,用起来却真香┗|`O′|┛ 嗷~~

    二、IO流概述

    I表示input、O表示output表示输入输出流。

    流是一种抽象的概念,是对数据传输的总称,也就是数据在设备间传输称为流,流的本质
    是数据传输,流的本质是数据传输。

    我们都知道,程序运行会加载到内存上。


    • IO流就是用来处理设备间数据传输的问题的
      IO流的分类大致可以分为两种:

    ①按数据流向

    • 输入流:读数据
    • 输出流:写数据

    ②按数据类型分

    • 字节输入流;字节输出流
    • 字符输入流;字符输出流
      一般来说IO流是按照数据类型分的

    文本文件一般用字符流,其他用字节流

    字节流是万能流,可以用于任意类型的文件。

    三、字节流写数据

    ①InputStream:

    • 这个抽象类是表示输入字节流的所有类的超类。

    ②OutputStream:

    • 这个抽象类是表示字节输出流的所有类的超类。
      他们的子类都是以其父类作为子类名的后缀:



    因为他是一个抽象类,所以我们写、读数据一般都用他们的直接子类。

    FileOutputStream和FileInputStream,前者用于写数据,后者用于读数据。

    FileOutputStream(String name):创建文件输入流以指定的名称写入文件

    public class FileOutputDemo {
        public static void main(String[] args) throws IOException {
            //创建字节输出流对象,这个子类都会抛出异常FileNotFoundException  
            FileOutputStream fos=new FileOutputStream("E:\\MyTest\\kong.txt");
            File f1=new File("E:\\MyTest\\kong.txt");
            System.out.println(f1.createNewFile());
            //write会抛出IOException异常,所以他所在得方法中得抛出异常throws也可以try..catch处理
            fos.write(49);//1
           fos.write(48); //0
           fos.write(48);//0
            byte arr[]=new byte[10];
            fos.write(arr);
            //释放资源
            fos.close();
        }
    }
    

    E:\MyTest\kong.txt,这个表示我创建在E盘下的Mytest模块下得kong.txt目录,当文件

    已经存会创建失败(false),返回的是boolean类型

    创建对象做了三件事情:

        调用系统功能创建文件。
        创建字节输出流对象。
        让字节输出流指向创建好的文件。
    

    凡是涉及到IO操做创建的对象都得释放资源。

    字节流写数据的三种方式:


    import java.io.*;
    
    public class FileOutputStreamDemo02 {
       public static void main(String[] args) throws IOException {
           //创建输入流对象
           FileOutputStream fos=new FileOutputStream("E:\\MyTest\\kong03.txt");
           FileOutputStream fos1=new FileOutputStream(new File("E:\\MyTest\\kong03.txt"));
           //上面两种方式等价
               //第一种方式写
           fos.write(97); fos.write(98); fos.write(99);
              //第二种方式写
           byte[]bys={97,98,99,100};//abcd
           fos.write(bys);
              //第三种方式写
           byte[]bys1="abcd".getBytes();
           fos.write(bys1, 1, 3);//bcd写在文件
           //释放资源
           fos.close();
       }
    }
    

    字节流写数据的两个小问题:

    有上面的操作可知,我们不难发现这写入的数据没有换行,这看着十分不舒服。

    ①字节流输入数据换行问题:

    windows操作系统下:\r\n
    Linux:\n
    mac:\r
    只需在写数据时加入对应的即可实现换行

    ②字节流写数据如何追加的问题

    只需要在创建指定路径后加true即可。

    public FileOutputSteam(String name,boolean append)
    import java.io.FileOutputStream;
    import java.io.IOException;
     
    public class FileTextZJ {
        public static void main(String[] args) throws IOException {
            FileOutputStream fos =new FileOutputStream("E:\\MyTest\\kong04.txt",true);
            for (int i=0;i<5;i++){
                fos.write("hello\r\n".getBytes());
     
            }
            fos.close();
        }
    }
    

    下面时运行两次的结果:,达到了我们像要的追加效果。


    字节流数据据
    1.一次读一个字节
    FileInputStream(String name)//写出从哪里读取
    没数据到达文件末尾时,返回-1,所以只需根据返回值即可

    public class FileInputStreamDemo{
     
       public static void main(String [] args) throw IoException{
         FileInputStream fis=new FileInputStream("E:MyByte\\kong02.txt");
         int by;
         while(by=fis.read()!=-1){
           System.out.println((char)by);
         }
         fis.close();
     
       }
     
    }
    

    2.一次读一个数组

    import java.io.FileInputStream;
    import java.io.IOException;
     
    public class FileInputStreamDemo01 {
        public static void main(String[] args) throws IOException {
            FileInputStream fis=new FileInputStream("E:\\MyTest\\kong1.txt");
            byte[]bys=new byte[1024];
            int len;
            while((len=fis.read())!=-1){
                System.out.println(new String(bys,0,len));
            }
            fis.close();
        }
    }
    

    四、字符缓冲流

    BufferedWriter
    BufferedReader



    缓冲流的参数需要一个FileWriter或FileReader对象

    字符缓冲流写文件

    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
     
    public class BufferedWriterDemo1 {
        public static void main(String[] args) throws IOException {
            BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\MyText\\KongChao.txt"));
            bw.write("hello\r\n");
            bw.write("world\r\n");
            bw.close();
        }
    }
    

    字符缓冲流读数据

    public class BufferedWriterDemo {
        public static void main(String[] args) throws IOException {
     
            BufferedReader br=new BufferedReader(new FileReader("E:\\MyTest\\kong.txt"));
            int ch;
            while((ch=br.read())!=-1){
                System.out.print((char)ch);
            }
            br.close();
        }
    }
    

    字符缓冲流特有功能

    BufferedWriter:
    void newLine();//相当于换行
    BufferedReader:
    public String readLine():读一行文件,不包括换行符、和终止符,若流已经到文件尾部,则为null
     将使用特有方法,输存结合
    
    import java.io.*;
     
    public class BufferedWriterDemo1 {
        public static void main(String[] args) throws IOException {
            
      //创建字符输入流
            BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\MyTest\\KongChao.txt"));
      //创建字符输出流      
            BufferedReader br= new BufferedReader(new FileReader("E:\\MyTest\\copy.txt"));
            String line;
            while((line=br.readLine())!=null){
                bw.write(line);bw.newLine();
                bw.flush();//刷新到文件上
            }
            //关闭资源
            bw.close();
            br.close();
    }  }
    

    相关文章

      网友评论

          本文标题:【Java】IO流的混乱关系,看得我直呼绝绝子

          本文链接:https://www.haomeiwen.com/subject/bidgfrtx.html