inputstream和outputstream的区别 struts2和springmvc的区别面试
原创

inputstream和outputstream的区别 struts2和springmvc的区别面试

好文
试试语音读文章

IO 流与其他外部设备进行数据交互。比如将数据从内存中保存到磁盘文件中或者从网络上下载数据并加载到内存中。这个过程都是一种单向且有顺序的数据传输。被称之为流。

IO 就是 Input 输入和 Output 输出。输入输出以内存为中心的流向划分的。传输数据到内存就是输入流。从内存中输出数据就是输出流。

InputStreamInputStream 是所有输入流的父类。是一个抽象类。读取的数据单位是字节(byte)。

主要的抽象方法是 read()。这个方法就是读取数据内容并返回 -1~255 的 int 值。read() 方法是一个阻塞的方法。只有将内容全部读取完成之后才能运行下一行代码。

public abstract int read() throws IOException;以 FileInputStream 实现类作为示例:

public static void main(String[] args) throws Exception { // input.txt 内容为 hello, inputStream InputStream input = new FileInputStream("input.txt"); int n = 0; while((n = input.read()) != -1){ System.out.print((char) n); } input.close();}示例中的 read() 方法只能一个字节的一个字节读取数据。效率不高。当文件中存在多个字节为一个汉字的中文时。上面的示例将打印出乱码。

InputStream 支持将一次性读取多个字节到缓冲区。利用缓冲区提升效率。返回值的数据不再是读取的数据字节。而是读取的字节数。并且可以正常的打印出中文字符。

// 将读取的内容填充到 byte 数组public int read(byte b[]) throws IOException// 将读取的内容填充 byte 数组中 off 开始。len 长度的位置public int read(byte b[], int off, int len) throws IOException将缓冲区大小设置为 1024 个字节示例:

public static void main(String[] args) throws Exception { //input.txt 文件内容为 你好, inputStream InputStream input = new FileInputStream("input.txt"); byte[] result = new byte[1024]; while(input.read(result) != -1){ System.out.print(new String(result, "utf-8")); } input.close();}OutputStreamOutputStream 是所有输出流的父类。和 InputStream 一样是一个抽象类。

主要的抽象方法是 write()。也是一个阻塞的方法。只有将内容全部写完成之后才能运行下一行代码。

write() 方法和 read() 方法一样都是一个字节一个字节的操作的。

public abstract void write(int b) throws IOException;以 FileInputStream 实现类作为示例:

public static void main(String[] args) throws Exception { OutputStream out = new FileOutputStream("out.txt"); try { out.write("h".getBytes()); out.write("e".getBytes()); out.write("l".getBytes()); out.write("l".getBytes()); out.write("0".getBytes()); } finally { if(out != null) { out.close(); } }}write() 按单个字节写入磁盘的效率比较低下。OutputStream 提供了 write(byte[]) 一次性大批量的将字节输出到磁盘。对于 IO 设备来探讨。一次性写入 1 个字节和写入 1000 个字节的期间都是差一点的。

public static void main(String[] args) throws Exception { OutputStream out = new FileOutputStream("out.txt"); try { out.write("哈喽。outputStream".getBytes()); } finally { if(out != null) { out.close(); } }}关闭资源不管是 InputStream 还是 OutputStream 在使用资源之后都需要调用 close()方法。在示例中如果在 close() 方法调用之前抛出异常则不会自动关闭资源。以下两种方式都可以关闭资源:

try..finally 方式public static void main(String[] args) throws Exception { InputStream input = new FileInputStream("input.txt"); try { byte[] result = new byte[1024]; while(input.read(result) != -1){ System.out.print(new String(result, "utf-8")); } } finally { if(input != null) { input.close(); } }}try(resource)实现了 Closeable 接口的 InputStream 和 OutputStream 使用 try(resource) 时。编译器会自动增加 finally。

public static void main(String[] args) throws Exception { try (OutputStream out = new FileOutputStream("out.txt")){ out.write("编译器会添加 finally".getBytes()); }}总结今天就是简单地给各位介绍 java 的 IO 流。为下一步学其他 IO 类打个基本。

您还感兴趣的文章推荐

以上就是由互联网推广工程师 网创网 整理编辑的,如果觉得有帮助欢迎收藏转发~

分享到 :
相关推荐

发表评论

您的电子邮箱地址不会被公开。

评论(2)

  • 公主抱 永久VIP 2022年12月14日 00:53:04

    inputstream和outputstream的区别 struts2和springmvc的区别面试 这篇解答确实也是太好了

  • 陌上柳絮 永久VIP 2022年12月14日 00:53:04

    字节,方法,数据,示例,都是,内容,是一个,内存,缓冲区,磁盘

  • 小新卖蜡笔 永久VIP 2022年12月14日 00:53:04

    IO 流与其他外部设备进行数据交互。比如将数据从内存中保存到磁盘文件中或者从网络上下载数据并加载到内存中。这个过程都是一