Java字节流

字节流:以 byte 为传输单位的 01 二进制数据

  • 输入还是输出针对的是内存

    输入流:外设 --> 内存

    输出流:内存 --> 外设


  • 字节流基类

    InputStream

    OutputStream

    具体功能依赖子类来实现


文件字节流

FileInputStream

FileOutputStream

  • FileOutputStream 文件写入

    • 主要构造方法

    • FileOutputStream(File file)

    • FileOutputStream(File file,boolean append) appendtrue 时表示追加写入,不覆盖

    • FileOutputStream(String name)

    • FileOutputStream(String name,boolean append) append同上

    • 主要成员方法

      若存在该文件则覆盖其内容,清除再写入

    • void write(int b) 单个字符写入,读一个写一个

    • void write(byte[] bytes) 按数组写入,读一个长度的数组写入一个长度

    • void write(byte[] bytes,int off,int len) 按数组,指定起始长度写入

  • JVM 之外的资源都需要显式释放 close(),因为 JVM 没有办法管理

//传入路径,覆盖写入
public void demo() throws IOException {
    FileOutputStream out = new FileOutputStream("./a.txt");
    out.write("hello".getBytes());
    //Java换行符
    //String s = System.lineSeparator();
    //win系统换行符 "\r\n"
    String s = "985\r\n";
    out.write(s.getBytes());
    out.write("world".getBytes());
    out.close();
}
//传入File对象,追加写入
public void demo2() throws IOException {
    FileOutputStream out = new FileOutputStream(new File("a.txt"), true);
    out.write("\r\n\r\ndadfadfadf".getBytes());
    out.close();
}

Java 换行符:System.lineSeparator()

win:\r\n

mac: \r

linux: \n


  • FileInputStream

    • 主要构造方法
    • FileInputStream(File file)
    • FileInputStream(String name)
    • 主要成员方法
    • int read() 读取一个字节
    • int read(byte[] b) 返回值是读取的字节个数
    • int read(byte[] b,int st,int dataCount)
    //单个读取
    public void demo() throws IOException {
            int readData;
            FileInputStream in = new FileInputStream(new File("a.txt"));
            while ((readData = in.read()) != -1) {
                System.out.print((char) readData);
            }
            in.close();
        }
    

```java //按数组循环读取 public void demo2() throws IOException { FileInputStream in = new FileInputStream(new File("a.txt")); byte[] bytes = new byte[1024]; int readCount; //每次读取返回的字节个数 while ((readCount = in.read(bytes)) != -1) { System.out.print(new String(bytes, 0, readCount)); } in.close(); } ```

包装流

  • 以底层流作为参数传递的流,称为包装流、处理流

  • 参数被称为底层流、节点流

  • 缓冲区:8kb

  • 只需要显式关闭包装流资源,包装流会自动关闭底层流资源

  • BufferedOutputStream

    • 主要构造方法

    • BufferedOutputStream(OutputStream out)

      传递的参数是字节输出流对象或者字节输出流子类对象

    • 主要成员方法

    • flush() 将数据从缓冲区刷新到文件中

    • close() 内部先调用 flush() 方法再释放资源

      可以不记得 flush() 但是一定要 close()!!!

  • BufferedInputStream

    • 主要构造方法

    • BufferedInputStream(InputStream in)

      传递的参数是字节输入流对象或者字节输入流子类对象

    • 主要成员方法

    • int read()InputStream 一样

    • int read(byte[] b,int off,int len)off 开始遍历 len 长字节读入指定 b字节数组

//单个字符复制
public static void copyImg(String src, String dst) throws IOException {
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(src)));
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(dst)));
    int readData;
    long start = System.currentTimeMillis();
    while ((readData = in.read()) != -1) {
        out.write(readData);
        out.flush();
    }
    long end = System.currentTimeMillis();
    System.out.println("单个字符复制视频需要" + (end - start) + "ms");
    in.close();
    out.close();
}
//多个字符复制
public static void copyImg2(String src, String dst) throws IOException {
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(src)));
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(dst)));
    int readCount;
    byte[] bytes = new byte[4096];
    long start = System.currentTimeMillis();
    while ((readCount = in.read(bytes)) != -1) {
        out.write(bytes,0,readCount);
    }
    long end = System.currentTimeMillis();
    System.out.println("按数组复制视频需要" + (end - start) + "ms");
    in.close();
    out.close();
}
posted @ 2021-02-18 22:52  海绵698  阅读(40)  评论(0)    收藏  举报