一、流的分类
- 数据类型
- 字节流:传输类型为字节
- 字符流:传输类型为字符
- 流向
- 输入流:读数据
- 输出流:写数据
为什么buffer比直接输入输出流快? 因为直接写每次调用都会触发系统调用,就会导致内核态切换,而缓冲它默认为8k,也就是每8k才会调用一次,速度就会快很多。
二、IO流类图
三、文件读取
public class FileBIO {/*** 字节流读取文件,需要关闭IO流*/@Testpublic void readByte() {File file = new File("e:\\test.log");FileInputStream inputStream = null;try {inputStream = new FileInputStream(file);int len;List<Byte> list = new ArrayList<>();while ((len = inputStream.read()) != -1) {list.add((byte) len);}byte[] bytes = new byte[list.size()];for (int i = 0; i < list.size(); i++) {bytes[i] = list.get(i);}System.out.println(new String(bytes, StandardCharsets.UTF_8));} catch (Exception e) {e.printStackTrace();} finally {if (inputStream != null) {try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}}}/*** 字节流读取文件,优雅方式,不用手动关闭流*/@Testpublic void readByteElegance() {File file = new File("e:\\610.log");try (FileInputStream inputStream = new FileInputStream(file)) {long begin = System.currentTimeMillis();int len;List<Byte> list = new ArrayList<>();while ((len = inputStream.read()) != -1) {list.add((byte) len);}byte[] bytes = new byte[list.size()];for (int i = 0; i < list.size(); i++) {bytes[i] = list.get(i);}System.out.println(new String(bytes, StandardCharsets.UTF_8));System.out.println(System.currentTimeMillis() - begin);} catch (Exception e) {e.printStackTrace();}}/*** 字节流读取文件,缓冲区读取字节流*/@Testpublic void readByteBuffer() {File file = new File("e:\\610.log");try (FileInputStream inputStream = new FileInputStream(file)) {long begin = System.currentTimeMillis();BufferedInputStream bis = new BufferedInputStream(inputStream);int len;List<Byte> list = new ArrayList<>();while ((len = bis.read()) != -1) {list.add((byte) len);}byte[] bytes = new byte[list.size()];for (int i = 0; i < list.size(); i++) {bytes[i] = list.get(i);}System.out.println(new String(bytes, StandardCharsets.UTF_8));System.out.println(System.currentTimeMillis() - begin);} catch (Exception e) {e.printStackTrace();}}/*** 字节流读取文件,缓冲区读取字节流,以数组的方式读取*/@Testpublic void readByteBufferArray() {File file = new File("e:\\610.log");try (FileInputStream inputStream = new FileInputStream(file)) {long begin = System.currentTimeMillis();BufferedInputStream bis = new BufferedInputStream(inputStream);int len;byte[] byt = new byte[1024];List<Byte> list = new ArrayList<>();while ((len = bis.read(byt)) != -1) {for (int i = 0; i < len; i++) {list.add(byt[i]);}}byte[] bytes = new byte[list.size()];for (int i = 0; i < list.size(); i++) {bytes[i] = list.get(i);}System.out.println(new String(bytes, StandardCharsets.UTF_8));System.out.println(System.currentTimeMillis() - begin);} catch (Exception e) {e.printStackTrace();}}/*** 字符读取文件*/@Testpublic void readChar() {File file = new File("e:\\610.log");try (FileInputStream inputStream = new FileInputStream(file)) {long begin = System.currentTimeMillis();InputStreamReader isr = new InputStreamReader(inputStream,StandardCharsets.UTF_8);int len = 0;StringBuilder builder = new StringBuilder();while ((len = isr.read()) != -1) {builder.append((char) len);}System.out.println(builder.toString());System.out.println(System.currentTimeMillis() - begin);} catch (Exception e) {e.printStackTrace();}}/*** 字符读取文件,通过缓冲流读取*/@Testpublic void readCharBuffer() {File file = new File("e:\\test.log");try (FileInputStream inputStream = new FileInputStream(file)) {long begin = System.currentTimeMillis();BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,StandardCharsets.UTF_8));String len;StringBuilder builder = new StringBuilder();while ((len = br.readLine()) != null) {//一行行读取的时候换行符就没有了,需要自己添加builder.append(len).append("\n");}builder.deleteCharAt(builder.length() - 1);System.out.println(builder.toString());System.out.println(System.currentTimeMillis() - begin);} catch (Exception e) {e.printStackTrace();}}}
