Java IO -- 字节流与字符流

概述

在程序中所有的数据都是以流的方式进行传输和保存的,程序需要数据时使用输入流读入数据,而当程序需要将一些数据保存起来时,就要使用输出流。
IO流根据流向分为输入流和输出流。根据数据类型分为字节流和字符流。字节流有字节输入流InputStream和字节输出流OutputStream,字符流有字符输入流Reader和字符输出流Writer

字节流

字节流主要操作 byte 类型数据,以 byte 数组为准,主要操作类是 OutputStreamInputStream

1.字节输出流 OutputStream

OutputStream 类是一个抽象类,如果要使用此类,则首先必须通过子类实例化对象。现在要操作一个文件,可用 FileOutputStream 类。
构造方法:

  • public FileOutputStream(File file)
    
  • public FileOutputStream(String str)
    

字节输出流操作步骤:

1.创建字节输出流对象
2.写数据
3.释放资源

案例一:向文件中写入字符串

public class IODemo1 {
    public static void main(String[] args) throws IOException {
        //创建字节输出流对象
        //方法1.public FileOutputStream(File file)
        File file = new File("D:\\fos.txt");
        FileOutputStream fos = new FileOutputStream(file);

        //方法2.public FileOutputStream(String str)
        FileOutputStream fos2 = new FileOutputStream("D:\\fos.txt");

        //写入数据:public void write(byte[] b)
        fos.write("HelloWorld".getBytes());
        //释放资源
        //关闭此字节输出流并释放与流相关的所有系统资源,此字节输出流不能再用于写入数据
        fos.close();
        /*
            为什么要关闭流:
                1.让流对象变成垃圾,这样就会被垃圾回收器回收
                2.通知系统去释放与该流相关的所有资源
         */
    }
}

FileOutputStream 写入的方法:

  •  public void write(byte[] b)
    
  •  public void write(byte[] b, int off, int len)
    
  •  public void write(int b)
    

案例二:

public class IODemo2 {
    public static void main(String[] args) throws IOException{
        //创建字节流对象
        FileOutputStream fos = new FileOutputStream("D:\\fos.txt");

        //写数据
        //1.public void write(int b)
        //fos.write(97);

        //2.public void write(byte[] b)
        byte[] bytes = {97, 98, 99, 100, 101};
        fos.write(bytes);//abcde

        //3.public void write(byte[] b, int off, int len)
        fos.write(bytes, 1, 2);//abcdebc

        //释放资源
        fos.close();
    }
}

写出的数据想要实现换行,Windows操作系统应该加上 \n
之前的所有操作中,如果重新执行程序,会覆盖文件中原有内容,那么想追加新内容的构造方法

  •  public FileOutputStream(File file, Boolean append)
    

如果将 append 的值设置为 true ,则表示在文件的末尾追加内容。

public class IODemo3 {
    public static void main(String[] args) throws IOException {
        //创建字节流对象
        FileOutputStream fos = new FileOutputStream("D:\\fos.txt", true);
        //写数据
        for (int i = 0; i < 10; i++) {
            fos.write(("helloJava" + i).getBytes());
            fos.write("\n".getBytes());
        }
        //释放资源
        fos.close();
    }
}

使用 try...catchIO 流进行异常处理:

public class IODemo4 {
    public static void main(String[] args) {
        //创建字节输出流对象
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("D:\\fos.txt");
            //写入数据
            fos.write("hello".getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {//必须有释放资源这一步,所以要写到finally里面
            //解决空指针问题
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

字节输入流 InputStream

字节输入流操作步骤:

1.创建字节输入流对象
2.写数据
3.释放资源

public class IODemo5 {
    public static void main(String[] args) throws IOException {
        //创建字节输入流对象
        FileInputStream fis = new FileInputStream("D:\\fos.txt");

        /*int content = fis.read();
        while (content != -1) {
            System.out.print((char)content);
            content = fis.read();
        }*/

        //也可以合在一起写
        int content = 0;
        while ((content = fis.read()) != -1) {
            System.out.print((char)content);
        }
        fis.close();
    }
}

文件的复制:
数据源 :将文件中的信息读取到Java程序中—FileInputStream
目的地 :将内存中的信息写入fos.txt中—FileOutputStream

public class IODemo6 {
    public static void main(String[] args) throws IOException {
        //分别创建输入输出流
        FileInputStream fis = new FileInputStream("D:\\fis.txt");
        FileOutputStream fos = new FileOutputStream("D:\\fos.txt");

        //读写操作
        int content = 0;
        while ((content = fis.read()) != -1) {
            fos.write(content);
        }

        //释放资源
        fis.close();
        fos.close();
    }
}

字符流

在程序中一个字符等于两个字节,Java提供了ReaderWriter两个专门操作字符流的类。

字符输出流 Writer

Writer 也是一个抽象类,如果要使用此类,则要使用其子类,此时是向文件中写入内容,应该使用 FileWriter 类。

public class IODemo7 {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("D:\\fw.txt");
        String str = "HelloWorld";
        fw.write(str);
        fw.close();
    }
}

整个程序与 OutputStream 没有太大的区别,好处是可以直接输出字符串,而不用将字符串变为 byte 数组后再输出。
使用 FileWriter 追加文件的内容,只需在创建 FileWriter 对象时将 append 属性的值设置为 true

        FileWriter fw = new FileWriter("D:\\fw.txt", true);

字符输入流 Reader

Reader 也是一个抽象类,如果要使用此类,则要使用其子类,此时是向文件中写入内容,应该使用 FileReader 类。

public class IODemo7 {
    public static void main(String[] args) throws IOException {
        FileReader fr = new FileReader("D:\\fw.txt");
        int content = 0;
        while ((content = fr.read()) != -1) {
            System.out.print((char) content);
        }
        fr.close();
    }
}

字节流与字符流的区别

实际上字节流在操作时本身不会不会用到缓冲区,是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件。

我是快斗,欢迎批评指正!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值