JavaIO详解--快速学懂字节流与字符流

从来好事天生俭,自古瓜儿苦后甜。这篇文章主要讲述JavaIO详解--快速学懂字节流与字符流相关的知识,希望能为你提供帮助。
一、字节流概述字节流分为字节输出流(outputStream)、字节输入流(inputStream)
字节流直接对二进制数据进行操作;字节流一般用来对媒体文件操作,比如图片。
二、字节输出流(outputStream)java中的outputStream是控制字节输出的抽象类,继承了Closeable, Flushable,AutoCloseable接口,因为是抽象类,所以在使用时我们必须使用他的子类对象实例化。
Closeable接口提供了close方法,该方法用于关闭流,Flushable提供了了flush方法,该方法用于清空缓存区,并把数据写到指定位置。
outputstream控制流的输出,所以他的主要方法是write

public abstract void write(int b)输出单个字节数据 public void write(byte[] b)输出字节数组的数据 public void write(byte[] b,int off,int len)输出字节数组指定位置的数据

【JavaIO详解--快速学懂字节流与字符流】我们现在通过outputStream的子类FileoutputStream进行文件的输出
FileOutputStream(File file) FileOutputStream(File file, boolean append)

上面展示了FileOutputStream的两个构造方法,第二个构造方法的第二个参数表示是覆盖还是添加数据,true为添加,false为覆盖。
public static void main(String[] args) throws IOException { //1.创建File对象 File file=new File("F:"+File.separator+"test.txt"); //2.通过子类进行父类对象的实例化 OutputStream stream=new FileOutputStream(file); //3.写文件 stream.write("xxx".getBytes()); //4.关闭流 stream.close(); }

通过四个步骤完成数据的输出
三、字节输入流(inputStream)java中的inputStream是控制字节输入的抽象类,继承了Closeable,AutoCloseable接口,你会发现和outputStream相比,少了一个Flushable接口,原因是字节输出的时候需要一个缓冲区存放即将写入的数据,而读取数据并不需要缓冲区。
因为inputStream是个抽象类,因此我们还是使用其子类,这里依旧通过FileInputStream来介绍
FileInputStream(File file)

inputStream的主要方法是read
int read()//读取一个字节,如果为空则返回-1 int read(byte[] b)//读取所有字节,返回字节长度,如果为空则返回-1 int read(byte[] b, int off, int len)//读取指定字节,返回字节长度,如果为空则返回-1 int available()//返回可以读到的字节长度

通过代码实际展示
public static void main(String[] args) throws IOException { File file=new File("F:"+File.separator+"test.txt"); //1.通过子类进行父类对象的实例化 InputStream stream=new FileInputStream(file); //2.设置读取容器 byte[] data=https://www.songbingjia.com/android/new byte[1024]; //3.读取数据 stream.read(data); System.out.println(new String(data)); //4.关闭流 stream.close(); }

四、字符流概述字节流在进行输出的时候使用的都是字节数组的形式,但是在大部分情况下,使用字符串的方式能更加简化输出操作,因此在jdk1.1之后出现了字符流。
五、字符输出流(Writer)Writer是控制字符输出的抽象类,它继承了Closeable, Flushable, Appendable,AutoCloseable接口,相比outputStream,这里多了一个Appendable接口,Appendable提供一个append方法用于追加输入。
Appendable append(char c) Appendable append(CharSequence csq) . Appendable append(CharSequence csq, int start, int end)

Writer中的方法主要也是write
void write(char[] cbuf) abstract void write(char[] cbuf, int off, int len) void write(int c) void write(String str) void write(String str, int off, int len)

这里用到最多的是void write(String str) 方法,我们通过Writer的子类FileWriter进行实践
public static void main(String[] args) throws IOException { //1.创建File对象 File file=new File("F:"+File.separator+"test.txt"); //2.通过子类进行父类对象的实例化 Writer writer=new FileWriter(file); //3.写入 writer.write("yyy"); //4.追加 writer.append("xxx"); //5.关闭 writer.close(); }

六、字符输入流(Reader)Reader是实现字符数据的输入流类,Reader继承了Closeable, AutoCloseable, Readable接口,Reader和InputStream类似,主要通过read()方法读取数据:
int read() int read(char[] cbuf) abstract int read(char[] cbuf, int off, int len) int read(CharBuffer target)

我们还是通过FileReader这个子类进行操作:
public static void main(String[] args) throws IOException { File file=new File("F:"+File.separator+"test.txt"); Reader reader=new FileReader(file); char[] data=https://www.songbingjia.com/android/new char[1024]; int len=reader.read(data); System.out.println(new String(data,0,len)); reader.close(); }

七、对比与总结前面的四种流均继承了AutoCloseable接口,因此我们还可以通过AutoCloseable来自动关闭类,关闭的方式就是使用try-catch方法。
public static void main(String[] args) throws IOException { try{ File file=new File("F:"+File.separator+"test.txt"); Reader reader=new FileReader(file); char[] data=https://www.songbingjia.com/android/new char[1024]; int len=reader.read(data); System.out.println(new String(data,0,len)); }catch (Exception e){ } }

关于何时使用哪种流进行操作的问题,字节流属于操作二进制数据流,因此在网络传输、图片等数据处理时更加常用。字节流不方便处理中文,因此中文或字符串的处理使用字符流。
JavaIO详解--快速学懂字节流与字符流

文章图片


    推荐阅读