IO流学习(上)

前言

IO流学习
javaApi中文

1、常用的文件操作

1.1 创建文件对象的相关构造器和方法

1new File(String path) //根据路径名构建一个File对象
2new File(File parent,String child)//根据父目录文件+子路径构建
3new File(String parent,String child)//根据父目录+子路径构建

createNewFile()//创建新文件的方法

方式一:

        String path = "news.txt";
        File file = new File(path);
        try {
            boolean newFile = file.createNewFile();
            System.out.println(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

方式二:

        File parent = new File("E:\\STUDYSPEACE\\activiti01\\");
        //这个对象是java的对象
        File file = new File(parent,"news2.txt");
        try {
        	//使用这个方法时,在会在硬盘中创建真正的文件
            boolean newFile = file.createNewFile();
            System.out.println(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

方式三:

        File file = new File("E:\\STUDYSPEACE\\activiti01\\","news3.txt");
        try {
            boolean newFile = file.createNewFile();
            System.out.println(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

1.2 获取文件的信息

File类有很多的方法,截图只是一部分
在这里插入图片描述
一些常用的方法:

        File file = new File("E:\\STUDYSPEACE\\activiti01\\news.txt");

        //调用方法,获取文件信息
        String name = file.getName();
        System.out.println("文件名:"+name);

        String absolutePath = file.getAbsolutePath();
        System.out.println("绝对路径:"+absolutePath);

        String parent = file.getParent();
        System.out.println("父级目录:"+parent);

        long length = file.length();
        System.out.println("文件大小:"+length);

        boolean exists = file.exists();
        System.out.println("文件是否存在:"+exists);

        boolean file1 = file.isFile();
        System.out.println("是否是文件:"+file1);

        boolean directory = file.isDirectory();
        System.out.println("是否是目录:"+directory);

1.3 目录操作

mkdir()//创建一级目录,如果用来创建多级目录会报错
mkdirs()//创建多级目录
        File file = new File("E:\\STUDYSPEACE\\activiti01\\file\\");
        file.mkdirs();

2、IO流类图

在这里插入图片描述

3、流的分类

3.1 输入流、输出流

输入输出是相较于内存而言的
输入流:数据从文件(磁盘)到内存。
输出流:数据从内存到文件(磁盘)。

3.2 字符流、字节流

按照传输的基本单位划分
字节流:在传输的过程中,传输的基本单位为字节。
字符流:在传输的过程中,传输的基本单位为字符。

3.3 节点流、处理流

节点流:可以从或向一个特定的地方(节点)读写数据
处理流:是对一个已存在的流的连接和封装,通过所封装的流的功能调用实现数据读写。
如BufferedReader。处理流的构造方法总是要带一个其他的流对象做参数

4、字节流

InputStream:字节输入流,它是一个抽象类,是所有子接输入流的超类

常用的子类:

  • FileInputStream:文件输入流
  • BufferedInputStream:缓冲字节输入流
  • ObjectInputStream:对象字节输入流
    在这里插入图片描述

4.1 FileInputStream读取文件示例

4.1.1 read()

在这里插入图片描述

        File file = new File("news.txt");
        FileInputStream fis = null;
        try{
            fis = new FileInputStream(file);
            int read = 0;
            //返回值为-1读取完毕
            while((read = fis.read()) != -1){
                System.out.println((char)read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
  1. 使用这种方式读取中文字符的文件会出现乱码,因为一次只读取了字符中的一个字节
  2. 读取的效率低

4.1.2 read(byte[] b)

在这里插入图片描述

        File file = new File("news.txt");
        FileInputStream fis = null;
        byte[] bytes = new byte[10];
        try{
            fis = new FileInputStream(file);
            int read = 0;
            //返回-1 读取完毕
            //如果读取长度不到定义的bytes的10字节,返回真正读取到的字节数
            while((read = fis.read(bytes)) != -1){
                //这里使用read 不要使用bytes.length
                System.out.println( new String(bytes,0,read));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

4.2 FileOutputStream示例

在这里插入图片描述

4.2.1 write()

在这里插入图片描述

        /**
         * FileOutputStream 将数据写入到文件,如果该文件不存在,就创建该文件
         */
        File file = new File("news.txt");
        FileOutputStream fos = null;
        byte[] bytes = new byte[10];
        try {
            String s = "hello world output stream";
            fos = new FileOutputStream(file);
            //字符串的getBytes方法,可以传入字符编码方式,这里不输入
            fos.write(s.getBytes());
        } catch (IOException fileNotFoundException) {
            fileNotFoundException.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

这种写入文件的方式,如果文件中存在内容,会将原有的内容覆盖
怎样在原有的内容上追加新的内容呢,只需要在构造器中增加一个参数就行了。
在这里插入图片描述
在这里插入图片描述

4.3 拷贝文件
将上面的FileInputStream与FileOutputStream综合运用一下,完成一个复制文件的小案例

        /**
         * 步骤
         * 1、创建文件输入流,读取文件
         * 2、创建文件输出流,写出文件到指定位置
         * 在复制的过程中,应该读取部分数据就写入到目标文件
         */
        File readFile = new File("11.gif");
        File saveFile = new File("12.gif");
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try{
            //定义一个字节数组,一次顶读取多个字节,提高效率
            byte[] bytes = new byte[1024];
            int len = 0;

            fis = new FileInputStream(readFile);
            fos = new FileOutputStream(saveFile);

            while((len = fis.read(bytes)) != -1){
                //写入到文件,要是用这个方法才能保证文件不会复制出错
                fos.write(bytes,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                assert fis != null;
                fis.close();
                assert fos != null;
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

5、字符流

5.1 FileReader

在这里插入图片描述在这里插入图片描述

		//单个字符读取
        FileReader frd = null;
        try {
            File file = new File("people.txt");
            frd = new FileReader(file);
            int len = 0;
            while ((len = frd.read()) != -1){
            	//因为读取结果是int所以应该转换为char在输出
                System.out.println((char)len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                assert frd != null;
                frd.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
		//多个字符读取
        FileReader frd = null;
        try {
            File file = new File("people.txt");
            frd = new FileReader(file);
            int len = 0;
            char[] chars = new char[10];
            while ((len = frd.read(chars)) != -1){
                System.out.println(new String(chars,0,len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
        ......
        }

5.2 FileWrite

在这里插入图片描述
FileWrite使用后,必须要flush或close,才能将数据写入到文件中

        /**
         * write(int)写入单个字符
         * write(char[])写入char数组
         * write(char[],off,len)写入数组的指定部分
         * write(string)写入字符串
         * write(string,off,len)写入字符串指定部分
         */
        FileWriter fw = null;
        try {
            File file = new File("people.txt");
            fw = new FileWriter(file,true);
            String s = "根本就是一狼";
            fw.write(s);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
        //一定要关闭或刷新流,否则数据无法写入
        ......
        }

6、处理流

BufferedReader、BufferedWriter

6.1 BufferedReader

在这里插入图片描述
在这里插入图片描述

        BufferedReader bufferedReader = null;
        FileReader fr = null;
        try{
            File file = new File("people.txt");
            fr = new FileReader(file);
            bufferedReader = new BufferedReader(fr);
            String line = "";
            //当readLine()读取到结尾时,会返回null
            while ((line = bufferedReader.readLine()) != null){
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                assert bufferedReader != null;
                //关闭外层的流
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

关闭处理流时只需要关闭外层的流就可以了。
在调用bufferedReader.close();时进入到close()方法中

public class BufferedReader extends Reader {

    private Reader in;

	.......
	//构造器传进来一个流参数
    public BufferedReader(Reader in, int sz) {
        super(in);
        if (sz <= 0)
            throw new IllegalArgumentException("Buffer size <= 0");
        this.in = in;
        cb = new char[sz];
        nextChar = nChars = 0;
    }
	......
	//close方法,其实是调用了传进来的流的close()方法
    public void close() throws IOException {
        synchronized (lock) {
            if (in == null)
                return;
            try {
                in.close();
            } finally {
                in = null;
                cb = null;
            }
        }
    }
    
}

6.2 BufferedWrite

在这里插入图片描述

        FileWriter fw = null;
        BufferedWriter bfw = null;
        try {
            File file = new File("people.txt");
            fw = new FileWriter(file);
            bfw = new BufferedWriter(fw);
            bfw.write("刀");
            bfw.newLine();
            bfw.write("刀了");
            bfw.newLine();
            bfw.write("黄老爷一来,钱就刀了");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            assert bfw != null;
            bfw.close();
        }

使用处理流想要在原文件追加信息,其实是要设置节点流的参数

            fw = new FileWriter(file,true);
            bfw = new BufferedWriter(fw);

6.3 字符处理流拷贝示例

这里读取的时字符文件,不要去操作二进制文件

        FileReader fr = null;
        FileWriter fw = null;
        BufferedReader bfr = null;
        BufferedWriter bfw = null;
        try {
            File file;
            fr = new FileReader("people.txt");
            fw = new FileWriter("people1.txt");
            bfr = new BufferedReader(fr);
            bfw = new BufferedWriter(fw);
            String line = "";
            while((line = bfr.readLine()) != null){
                bfw.write(line);
                //如果不插入换行,所有的内容会在一行
                bfw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            assert bfr != null;
            bfr.close();
            assert bfw != null;
            bfw.close();
        }

6.4 字节处理流示例

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        InputStream is = null;
        OutputStream os = null;
        try {
            File file;
            is = new FileInputStream("11.gif");
            bis = new BufferedInputStream(is);

            os = new FileOutputStream("13.gif");
            bos = new BufferedOutputStream(os);
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = bis.read(bytes)) != -1){
                bos.write(bytes,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            bis.close();
            bos.close();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值