字节流简述及应用

来源:互联网 发布:简繁转换软件 编辑:程序博客网 时间:2024/06/06 04:44

Java中的流分为两种,一种是字节流,另一种是字符流,分别由四个抽象类来表示:InputStream,OutputStream,Reader,Writer。Java中其他多种多样变化的流均是由它们派生出来的。

今天就整理下字节流的应用~
字符流跟字节流区别:
1.字符流只能读取字符,比如:.txt的文件
2.字节流可以读取一切文件类型,比如:.avi .jpg等等

注意:在java程序中,将文件读取到内存视为input
将文件写出到文件中视为output

打开API文档,我们可以看到最顶层是InputStream和OutPutStream两个接口,也就是说,我们要应用的话得去找他的子类

这里写图片描述

所以,应用也是围绕这这俩类来的。
OutputStream的API:
这里写图片描述
FileOutputStream的构造方法:
这里写图片描述
FileOutputStream的追加续写方法:
这里写图片描述
好了,写入数据就需要这么几个方法!
来上代码:将数据写入到文件中。

public class FileOutputStreamDemo {    public static void main(String[] args) throws IOException {        //需求:将数据写入到文件中。        //创建存储数据的文件。        File file = new File("c:\\ file.txt");        //创建一个用于操作文件的字节输出流对象。一创建就必须明确数据存储目的地。        //输出流目的是文件,会自动创建。如果文件存在,则覆盖。        FileOutputStream fos = new FileOutputStream(file);        //调用父类中的write方法。        byte[] data = "abcde".getBytes();        fos.write(data);        //关闭流资源。        fos.close();    }}

再来个追加写入的

public class FileOutputStreamDemo2 {    public static void main(String[] args) throws Exception {        File file = new File("c:\\file.txt");        FileOutputStream fos = new FileOutputStream(file, true);        String str = "\r\n"+"abcdef";        fos.write(str.getBytes());        fos.close();    }}

InputStream的API:
这里写图片描述
FileInputStream的构造方法:
这里写图片描述
FileInputStream的读取方法:
这里写图片描述
这里写图片描述

同样,读取一个文件也不需要太多的方法。

上代码:
以一个字节一个字节的读取方式读取一个文件的内容

public class FileInputStreamDemo {    public static void main(String[] args) throws IOException {        File file = new File("c:\\file.txt");        //创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。        FileInputStream fis = new FileInputStream(file);        //读取数据。使用 read();一次读一个字节。        int ch = 0;        while((ch=fis.read())!=-1){            System.out.println("ch="+(char)ch);        }        // 关闭资源。        fis.close();    }}

以读取数组的方式读取文件!

public class FileInputStreamDemo2 {    public static void main(String[] args) throws IOException {        /*         * 演示第二个读取方法, read(byte[]);         */        File file = new File("c:\\file.txt");        // 创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。        FileInputStream fis = new FileInputStream(file);                //创建一个字节数组。        byte[] buf = new byte[1024];//长度可以定义成1024的整数倍。              int len = 0;        while((len=fis.read(buf))!=-1){            System.out.println(new String(buf,0,len));        }        fis.close();    }}

综上所述:做一个综合的小例子

public class FileTest {    public static void main(String[] args) throws IOException {        //1,明确源和目的。        File srcFile = new File("c:\\test.JPG");        File destFile = new File("copyTest.JPG");        //2,明确字节流 输入流和源相关联,输出流和目的关联。        FileInputStream fis = new FileInputStream(srcFile);        FileOutputStream fos = new FileOutputStream(destFile);        //3, 使用输入流的读取方法读取字节,并将字节写入到目的中。        int ch = 0;        while((ch=fis.read())!=-1){            fos.write(ch);        }        //4,关闭资源。        fos.close();        fis.close();    }}

好了,这就是字节流的用法,但是,在我们去学字符流的时候会有缓冲流,那字节流有没有呢!
答案当然是有了!
字节缓冲流根据流的方向,共有2个
1. 写入数据到流中,字节缓冲输出流 BufferedOutputStream
2 . 读取流中的数据,字节缓冲输入流 BufferedInputStream
它们的内部都包含了一个缓冲区,通过缓冲区读写,就可以提高了IO流的读写速度
下面用缓冲流写个字节缓冲输出流例子:

public class BufferedOutputStreamDemo01 {    public static void main(String[] args) throws IOException {         //写数据到文件的方法        write();    }    /*     * 写数据到文件的方法     * 1,创建流     * 2,写数据     * 3,关闭流     */    private static void write() throws IOException {        //创建基本的字节输出流        FileOutputStream fileOut = new FileOutputStream("abc.txt");        //使用高效的流,把基本的流进行封装,实现速度的提升        BufferedOutputStream out = new BufferedOutputStream(fileOut);        //2,写数据        out.write("hello".getBytes());        //3,关闭流        out.close();    }}

在用缓冲流写个字节缓冲输入流例子:

/*     * 从文件中读取数据     * 1,创建缓冲流对象     * 2,读数据,打印     * 3,关闭     */    private static void read() throws IOException {        //1,创建缓冲流对象        FileInputStream fileIn = new FileInputStream("abc.txt");        //把基本的流包装成高效的流        BufferedInputStream in = new BufferedInputStream(fileIn);        //2,读数据        int ch = -1;        while ( (ch = in.read()) != -1 ) {            //打印            System.out.print((char)ch);        }        //3,关闭        in.close();    }

这就是字节流及缓冲流的用法,学起来只要明确流的方向,明确思路与方法的用法,其实很简单!(废话。。。=-=)