javaSE--核心之一:IO流

Java IO流框架结构:

 

 

 IO的主要内容包括输入、输出两种IO流,这两种流中又分为字节流和字符流,字节流是以字节为单位来处理输入、输出流,而字符流是以字符为单位来处理输入、输出流。

 

 

 

InputStream 类:

public abstract int read()    读取数据
public int read(byte b[])    将读取到的数据放在 byte 数组中,该方法实际上是根据下面的方法实现的,off 为 0,len 为数组的长度
public int read(byte b[], int off, int len)    从第 off 位置读取 len 长度字节的数据放到 byte 数组中,流是以 -1 来判断是否读取结束的(注意这里读取的虽然是一个字节,但是返回的却是 int 类型 4 个字节,这里当然是有原因,这里就不再细说了,推荐这篇文章,链接)
public long skip(long n)    跳过指定个数的字节不读取,想想看电影跳过片头片尾
public int available()    返回可读的字节数量
public void close()    读取完,关闭流,释放资源
public synchronized void mark(int readlimit)    标记读取位置,下次还可以从这里开始读取,使用前要看当前流是否支持,可以使用 markSupport() 方法判断
public synchronized void reset()    重置读取位置为上次 mark 标记的位置
public boolean markSupported()    判断当前流是否支持标记流,和上面两个方法配套使用

OutputStream 类:

 

1 public abstract void write(int b)    写入一个字节,可以看到这里的参数是一个 int 类型,对应上面的读方法,int 类型的 32 位,只有低 8 位才写入,高 24 位将舍弃。
2 public void write(byte b[])    将数组中的所有字节写入,和上面对应的 read() 方法类似,实际调用的也是下面的方法。
3 public void write(byte b[], int off, int len)    将 byte 数组从 off 位置开始,len 长度的字节写入
4 public void flush()    强制刷新,将缓冲中的数据写入
5 public void close()    关闭输出流,流被关闭后就不能再输出数据了

Reader 类:

 1 public int read(java.nio.CharBuffer target)    读取字节到字符缓存中
 2 public int read()    读取单个字符
 3 public int read(char cbuf[])    读取字符到指定的 char 数组中
 4 abstract public int read(char cbuf[], int off, int len)    从 off 位置读取 len 长度的字符到 char 数组中
 5 public long skip(long n)    跳过指定长度的字符数量
 6 public boolean ready()    和上面的 available() 方法类似
 7 public boolean markSupported()    判断当前流是否支持标记流
 8 public void mark(int readAheadLimit)    标记读取位置,下次还可以从这里开始读取,使用前要看当前流是否支持,可以使用 markSupport() 方法判断
 9 public void reset()    重置读取位置为上次 mark 标记的位置
10 abstract public void close()    关闭流释放相关资源

Writer 类:

 1 public void write(int c)    写入一个字符
 2 public void write(char cbuf[])    写入一个字符数组
 3 abstract public void write(char cbuf[], int off, int len)    从字符数组的 off 位置写入 len 数量的字符
 4 public void write(String str)    写入一个字符串
 5 public void write(String str, int off, int len)    从字符串的 off 位置写入 len 数量的字符
 6 public Writer append(CharSequence csq)    追加吸入一个字符序列
 7 public Writer append(CharSequence csq, int start, int end)    追加写入一个字符序列的一部分,从 start 位置开始,end 位置结束
 8 public Writer append(char c)    追加写入一个 16 位的字符
 9 abstract public void flush()    强制刷新,将缓冲中的数据写入
10 abstract public void close()    关闭输出流,流被关闭后就不能再输出数据了

字节流:文件读写:

字节流:一次读取一个字节
// 测试文件大小:2087KB
// 普通字节流,一次读取一个字节
public static void ioOption() throws IOException {
 
    // 获取文件 字节输入流,如果文件不存在抛出异常
    FileInputStream fis = new FileInputStream(new File("D:/demo.txt"));
    // 获取文件 字节输出流,如果文件不存在自动创建文件
    FileOutputStream fos = new FileOutputStream(new File("D:/copy.txt"));
 
    int len;
    // 一次读取一个字节
    // len 表示读取到的字节数,如果没有数据则返回 -1
    while ((len = fis.read())!= -1){
        fos.write(len);
    }
 
    fis.close();
    fos.close();
}

 

// 测试文件大小:2087KB
// 缓冲字节流,一次读取一个字节
public static void ioOption() throws IOException {
 
    // 获取文件 字节输入流,如果文件不存在抛出异常
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("D:/demo.txt")));
    // 获取文件 字节输出流,如果文件不存在自动创建文件
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:/copy.txt")));
 
    int len;
    // 一次读取一个字节
    // len 表示读取到的字节数,如果没有数据则返回 -1
    while ((len = bis.read())!= -1){
        bos.write(len);
    }
 
    bis.close();
    bos.close();

}

一次读一个不实用:读写全部的::

 1 // 测试文件大小:2087KB
 2 public static void ioOption() throws IOException {
 3  
 4     // 获取文件 字节输入流,如果文件不存在抛出异常
 5     FileInputStream fis = new FileInputStream(new File("D:/demo.txt"));
 6     // 获取文件 字节输出流,如果文件不存在自动创建文件
 7     FileOutputStream fos = new FileOutputStream(new File("D:/copy.txt"));
 8     // 字节数组
 9     byte[] bytes = new byte[fis.available()];
10  
11     int len = 0;
12     // 一次读取一个 bytes 大小的数据
13     // len 表示读取到的字节数,如果没有数据则返回 -1
14     while ((len = fis.read(bytes))!= -1){
15         fos.write(bytes,0,len);
16     }
17  
18     fis.close();
19     fos.close();
20 
21 }

图片写入:

//字节流方法
    public static void copyFile()throws IOException {

        //1.获取目标路径
        //(1)可以通过字符串
//        String srcPath = "C:\\Users\\bg\\Desktop\\截图笔记\\11.jpg";
//        String destPath = "C:\\Users\\bg\\Desktop\\图片备份\\11.jpg";

        //(2)通过文件类
        File srcPath = new File("C:\\Users\\bg\\Desktop\\截图笔记\\22.PNG");
        File destPath = new File("C:\\Users\\bg\\Desktop\\图片备份\\22.PNG");

        //2.创建通道,依次 打开输入流,输出流
        FileInputStream fis = new FileInputStream(srcPath);
        FileOutputStream fos = new FileOutputStream(destPath);

        byte[] bt = new byte[1024];

        //3.读取和写入信息(边读取边写入)
        while (fis.read(bt) != -1) {//读取
            fos.write(bt);//写入
        }

        //4.依次 关闭流(先开后关,后开先关)
        fos.close();
        fis.close();
    }

更加全的图片复制:

//以复制图片为例,实现try{ }cater{ }finally{ } 的使用
        public static void test(){
            //1.获取目标路径
            File srcPath = new File("C:\\Users\\bg\\Desktop\\截图笔记\\55.PNG");
            File destPath = new File("C:\\Users\\bg\\Desktop\\图片备份\\55.PNG");
            //2.创建通道,先赋空值
            FileInputStream fis = null;
            FileOutputStream fos = null;
            //3.创建通道时需要抛出异常
            try {
                fis = new FileInputStream(srcPath);
                fos = new FileOutputStream(destPath);

                byte[] bt = new byte[1024];
                //4.读取和写入数据时需要抛出异常
                try {
                    while(fis.read(bt) != -1){
                        fos.write(bt);
                    }
                } catch (Exception e) {
                    System.out.println("储存盘异常,请修理");
                    throw new RuntimeException(e);
                }


            } catch (FileNotFoundException e) {
                System.out.println("资源文件不存在");
                throw new RuntimeException(e);
            }finally{

                //5.无论有无异常,需要关闭资源(分别抛出异常)
                try {
                    fos.close();
                } catch (Exception e) {
                    System.out.println("资源文件或目标文件关闭失败!");
                    throw new RuntimeException(e);
                }

                try {
                    fis.close();
                } catch (IOException e) {
                    System.out.println("资源文件或目标文件关闭失败!");
                    throw new RuntimeException(e);
                }

            }
        }

 

 

 

 

posted on 2020-07-27 09:44  白嫖老郭  阅读(237)  评论(0编辑  收藏  举报

导航