Java文件流

这篇文章主要用来介绍Java文件流的一些操作,相信很多小伙伴在文件操作上还是处于懵懵懂懂的状态,没关系相信看了我这篇文章,肯定会有所收获

说明

其实Java的流操作有很多,文件流只是其中一部分,但是他们的操作方法大同小异,而且我们平时用到的最多的也就是文件流,所以我这里就只介绍文件流

字节输入输出流

InputStream 和 OutputStream 是所有字节输入输出流的父类,InputStream 是输入流,OutputStream是输出流。FileInputStream 和 FileOutputStream 只是他们的一个子类,下面来介绍一下它们的具体使用方法

FileInputStream构造方法

  • FileInputStream(File file) 传入一个File类的文件
  • FileInputStream(String name) 传入文件路径

常用方法

  • read() 读取一个字节,返回读取的字节数(1)
  • read(byte[] b) 向输入流中读取 自己数组b的长度个字节,返回读取的字节数(最常用),b 是用来存放读取的数据
  • read(byte[] b,int off,int len) 从下标off开始,读取len长度个字节

FileOutputStream构造方法与前面一样,这里不作赘述

常用方法将前面read的改成write其余都一样,下面直接用例子来演示

    private static void byteOutputStream() {
        //创建一个文件
        File file = new File("C:\\Users\\Knight\\Desktop\\Test\\b.txt");
        try {
            FileOutputStream fos = new FileOutputStream(file);
            String content = "hello world!";
            //将字符串转换成字节数组
            byte[] b = content.getBytes(StandardCharsets.UTF_8);
            fos.write(b);
            System.out.println("写入成功");
            //关闭流
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void byteInputStream() {
        File file = new File("C:\\Users\\Knight\\Desktop\\Test\\a.txt");
        try {
            FileInputStream fis = new FileInputStream(file);
            int count = 0;
            byte[] b = new byte[100];
            //当读到文件末尾时,fis.read()方法会返回-1
            while ((count = fis.read(b))!=-1){
                //将字节数组转换成字符串
                String s = new String(b,0,count);
                System.out.println(s);
            }
            //一定记得关闭流
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

字符输入输出流

字节输入输出流的缺陷在于它只能按字节读取和写入文件,而一个中文是占两个字节,所以如果操作不当就会出现乱码的情况,解决办法就是使用字符输入输出流。同字节输入输出流一样,FileReader 和 FileWriter 只是Reader 和 Writer 的一个子类。

构造方法

  • FileReader(File file) 传入一个File类对象
  • FileReader(String name) 传入文件路径

常用方法

  • read() 读取一个字符,返回1,若到了文件末尾返回-1
  • read(char[] buffer) 读取buffer长度的字符,并存到buffer中,返回读取的长度,到了文件末尾返回-1
  • read(char[] buffer,int off,int len) 同FileInputStream(byte[] b,int off,int len)

FileWriter的构造方法与常用方法与FileReader类似,这里不做赘述,直接写一个例子演示

    private static void charOutputStream() {
        String content = "中国万岁!";
        //将字符串转换成字符数组
        char[] c = content.toCharArray();
        File file = new File("C:\\Users\\Knight\\Desktop\\Test\\c.txt");
        try {
            FileWriter fw = new FileWriter(file);
            fw.write(c);
            fw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void charInputStream() {
        File file = new File("C:\\Users\\Knight\\Desktop\\Test\\c.txt");
        char[] c = new char[100];
        int count = 0;
        try {
            FileReader fr = new FileReader(file);
            while ((count = fr.read(c))!=-1){
                //将字符数组转换成字符串
                String s = new String(c,0,count);
                System.out.println(s);
            }
            fr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

在这里插入图片描述

缓冲区

还记得之前两个例子中都出现一个函数close()吗?这个函数非常重要!这个函数非常重要!这个函数非常重要!在对文件流操作完成之后一定要加上close(),即使程序结束后会自动关闭流。这里解释一下,因为我们的程序不管是读取文件还是写文件,都不是直接操作内存的,而是先将文件的内容放在缓冲区,缓冲区中的数据到达一定规模后就会将内容输送到输入输出流中,而close()函数一调用就是要求缓冲区中数据全部送出,如果你不手动调用这个函数很可能会出现数据丢失的现象。

缓冲流

细心的小伙伴有没有发现前面的两个例子写文件就是把你定义的数组中的内容全部一起写进文件中,读取文件也是将文件中的内容全部读取出来。那么问题来了,如果不想读取全部数据,只想读取一行怎么办?这时候就出现了缓冲流, 缓冲流增强了读写文件的能力,下面介绍一下缓冲流。

构造方法

  • BufferedReader(Reader in) 参数必须是字符输入流
  • BufferedWriter(Writer out) 参数必须是字符输出流

常用方法

  • readLine() 读取文件中的一行,若是文件结束返回null
  • write(String s) 将字符串s写到文件中
  • write(String s,int off,int len) 将字符串部分写到文件中

测试用例

 private static void bufferReader() {
        File file = new File("C:\\Users\\Knight\\Desktop\\Test\\c.txt");
        try {
        	//创建字符输入流对象
            FileReader fr = new FileReader(file);
            //创建缓冲流对象
            BufferedReader br = new BufferedReader(fr);
            String s = null;
            while ((s = br.readLine())!=null){
                System.out.println(s);
            }
            //从上到下一次关闭流
            br.close();
            fr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void bufferWirter() {
        File file = new File("C:\\Users\\Knight\\Desktop\\Test\\c.txt");
        String[] content = new String[]{"猪八戒","孙悟空","沙悟净","唐三藏"};
        try {
            FileWriter fr = new FileWriter(file);
            BufferedWriter bw = new BufferedWriter(fr);
            for (String s : content){
                bw.write(s);
                bw.newLine();
            }
            bw.close();
            fr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

在这里插入图片描述

结束语

好了,本次的文章就到这里了,希望对大家能有所帮助,有问题欢迎评论留言指正,谢谢大家的阅读。

  • 12
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小猪不吃草

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值