Java基础I/O流

前言:为什么要写这篇文章,由于长时间不使用I/O流操作,导致很多知识都朦朦胧胧似懂非懂,今天抽空把这部分内容认真复习了下,顺便写篇文章加深印象。I/O流在很多场景中都有使用,比如文件上传下载、socket通信等。本文主要介绍常用字节流,其它字符流、转换流用法类似就不一一介绍。

一、初始IO流

数据的传输,可以看做是一种数据的流动,按照流动的方向,以内存为基准,分为输入input和输出output,即流向内存的是输入流,流出内存的是输出流。

2、IO流的分类

按照流的方向可以分为:输入流和输出流

按照流的类型可以分为:字节流和字符流

字节流和字符流的超类如下所示

流类型

输入流

输出流

字节流

InputStream

OutputStream

字符流

Reader

Writer

字节流和字符流不同之处

读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。

处理对象不同:字节流能处理所有类型的数据(如图片、视频、音频等),而字符流只能处理字符类型的数据。

总结:只要是处理纯文本数据,就优先考虑使用字符流。 除此之外都使用字节流

字节流和字符流实现类汇总

网上找了一个

二、字节输入输出流

一切文件数据(文本、图片、视频等)在存储时,都是以二进制数字的形式保存,都一个一个的字节,那么传输时一样如此。所以,字节流可以传输任意文件数据。

1、字节输入流(InputStream)

InputStream抽象类字节输入流的所有类的超类,可以读取字节信息到内存中。它定义了字节输入流的基本共性功能方法。


1、public abstract int read();

2、 public void close();

3、public long skip(long n);

4、public synchronized void mark(int readlimit);

5、public synchronized void reset();


我们以FileInputStream读取文件为例


FileInputStream读取字节数据

test.txt文件内容为:123123123

1、读取字节:read方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回-1,代码如下:


public static void testFileInPut()throws Exception {
        FileInputStream inputStream = new FileInputStream("D:aplustest.txt");
        int b;
        while ((b = inputStream.read()) != -1) {
            System.out.println((char)b);
        }
        inputStream.close();
}

2、使用字节数组读取:read(byte[] b),每次读取b的长度个字节到数组中,返回读取到的有效字节个数,读取到末尾时,返回-1 ,代码如下:


public static void testFileInPut1()throws Exception {
        FileInputStream inputStream = new FileInputStream("D:aplustest.txt");
        int b;
        byte[] bytes = new byte[2];
        while ((b = inputStream.read(bytes)) != -1) {
            System.out.println(b);
            System.out.println("======");
            System.out.println(new String(bytes));
        }
        inputStream.close();
}


输出结果:

12

31

23

12

32

我们发现结果不正确,最后一个为啥是32,这是由于最后一次读取时,只读取一个字节3,数组中,上次读取的数据没有被完全替换,所以要通过len ,获取有效的字节。

每次读取后,把数组的有效字节部分,变成字符串打印

代码改造如下:


public static void testFileInPut1()throws Exception {
        FileInputStream inputStream = new FileInputStream("D:aplustest.txt");
        int b;
        byte[] bytes = new byte[2];
        while ((b = inputStream.read(bytes)) != -1) {
            //System.out.println(new String(bytes));
            System.out.println(new String(bytes,0,b));
        }
        inputStream.close();
}

2、字节输出流(OutputStream)

OutputStream抽象类输出流是所有类的超类(父类),将指定的字节信息写出到目的地。定义了字节输出流公共方法。


1、public abstract void write(int b)

2、public void write(byte b[])

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

4、 public void flush()

5、public void close()

我们以FileOutputStream写出文件为例

使用FileOutputStream写出字节数据主要通过Write方法,而write方法分如下三种

public void write(int b)

public void write(byte[] b)

public void write(byte[] b,int off,int len) //从`off`索引开始,`len`个字节


1、write(int b)每次写出一个字节数据,代码如下:

public static void testFileOutPut()throws Exception {
        FileOutputStream outputStream = new FileOutputStream("D:aplusout.txt");
        outputStream.write(98);
        outputStream.write(99);
        outputStream.flush();
        outputStream.close();
}

2、写出字节数组:write(byte[] b),每次可以写出数组中的数据,代码如下:

 public static void testFileOutPut1()throws Exception {
        FileOutputStream outputStream = new FileOutputStream("D:aplusout.txt");
        byte[] b = "我是java高手".getBytes();
        outputStream.write(b);
        outputStream.close();
}

3、写出指定长度字节数组:write(byte[] b, int off, int len) ,每次写出从off索引开始,len个字节,代码如下:


 public static void testFileOutPut2()throws Exception {
        byte[] b = "java".getBytes();
        outputStream.write(b,0,2);
        outputStream.close();
}

FileOutputStream实现数据追加续写

FileOutputStream提供了一个构造函数FileOutputStream(File file, boolean append),当boolean为true时表示追加数据。


 public static void testFileOutPut3()throws Exception {
        FileOutputStream outputStream = new FileOutputStream("D:aplusout.txt",true);
        byte[] b = "java".getBytes();
        outputStream.write(b);
        outputStream.close();
}


3、文件拷贝

上面已经详细介绍了字节输入和输出流,我们现在做一个例子实现图片的拷贝。

上面的写法不规范,下面我们写一个规范的文件拷贝,代码如下:

public static void filePhotoCopy() {
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
             inputStream = new FileInputStream("D:aplusIO流.png");
             outputStream = new FileOutputStream("D:aplusIOCopy.png");
             byte[] b = new byte[1024];
             int i;
             while ((i = inputStream.read(b)) != -1) {
                 outputStream.write(b,0,i);
                 outputStream.flush();
             }
            System.out.println("拷贝完成");
        }  catch (Exception e) {
        } finally {
              if (inputStream != null) {
                  try {
                      inputStream.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              if (outputStream != null) {
                  try {
                      outputStream.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
        }
}

三、处理流和转换流

处理流和节点流一块使用,在节点流的基础上,再套接一层,套接在节点流上的就是处理流。

常用的处理流

展开阅读全文

页面更新:2024-05-15

标签:数组   字节   字符   内存   类型   单位   代码   文件   基础   方法   数据

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top