博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Core Java - 流(Stream) - 字节流和字符流(一)
阅读量:6967 次
发布时间:2019-06-27

本文共 5595 字,大约阅读时间需要 18 分钟。

0. 概述:

Java中基于流的I/O构建在4个抽象类之上, 其中2个是字节流,另外2个是字符流。

字节流:

InputStream / OutputStream

当操作字节或其它二进制对象时,应当使用字节流。

 

字符流:

Reader / Writer

当操作字符或字符串时,应当使用字符流。

 

1. InputStream

InputStream:   输入字节流,它是一个抽象类。

常用主要方法:

int read()

返回代表下一个可用字节的整数,当文件达到末尾时,返回-1。

 

int read(bute buffer[])

尝试读取buffer.length个字节到buffer中,返回实际成功读取的字节数;文件到末尾返回-1。

 

int read(bute buffer[], int offset, int numBytes)

尝试读取numBytes个字节到buffer中,从buffer[offset]开始保存读取的字节;

返回实际成功读取的字节数;文件到末尾返回-1。

 

2. OutputStream

OutputStream: 输出字节流,它是一个抽象类。

OutputStream有很多子类,它们分别负责向不同的介质写入数据

例如:

FileOutputStream           写入数据到文件

TelnetOutputStream       写入数据到网络连接

ByteArrayOutputStream  写入数据到字节数组

 

常用主要方法:

void write(int b)

向输出流中写入单个字节。

 

void write(byte buffer[])

向输出流中写入一个完整的字节数组。

 

void write(byte buffer[], int offset, int numBytes)

从buffer[offset]开始将numBytes个字节写入输出流中。

 

void flush()

结束输出状态,刷新输出缓冲区。

 

3. FileInputStream

文件读取字节流。

常用构造函数:

FileInputStream(String filePath)  (更常用)

FileInputStream(File fileObj)

一个同构输入流来读取文件内容的例子,演示了3种方式(单字节读取, 字节数组读取, 忽略部分字节读取)

代码如下:

package corejava8.io.stream;import java.io.*;public class FileInputStreamDemo {    public static void main(String args[]) {        int size;        // Use try-with-resources to close the stream        try (FileInputStream f = new FileInputStream("src/corejava8/io/stream/FileInputStreamDemo.java")) {            System.out.println("Total Available Bytes: " + (size = f.available()));            int n = size / 40;                        // 通过循环每次单字节读取            System.out.println("First " + n + " bytes of the file one read() at a time");            for (int i = 0; i < n; i++) {                System.out.print((char) f.read());            }            System.out.println("\nStill Available: " + f.available());            System.out.println("--------------------------------------------------------------");                        // 字节数组读取            System.out.println("Reading the next " + n + " with one read(b[])");            byte b[] = new byte[n];            if (f.read(b) != n) {                System.err.println("couldn’t read " + n + " bytes.");            }            System.out.println(new String(b, 0, n));            System.out.println("\nStill Available: " + (size = f.available()));            System.out.println("--------------------------------------------------------------");                        // 忽略部分字节读取            System.out.println("Skipping half of remaining bytes with skip()");            f.skip(size / 2);            System.out.println("Still Available: " + f.available());            System.out.println("Reading " + n / 2 + " into the end of array");            if (f.read(b, n / 2, n / 2) != n / 2) {                System.err.println("couldn't read " + n / 2 + " bytes.");            }            System.out.println(new String(b, 0, b.length));            System.out.println("\nStill Available: " + f.available());        } catch (IOException e) {            System.out.println("I/O Error: " + e);        }    }}

运行结果如下:

Total Available Bytes: 1704

First 42 bytes of the file one read() at a time

package corejava8.io.stream;

 

import java.

Still Available: 1662

--------------------------------------------------------------

Reading the next 42 with one read(b[])

io.*;

 

public class FileInputStreamDemo {

 

 

Still Available: 1620

--------------------------------------------------------------

Skipping half of remaining bytes with skip()

Still Available: 810

Reading 21 into the end of array

io.*;

 

public class Fm.err.println("couldn

 

Still Available: 789

 

4. FileOutputStream

文件输出字节流,用于向文件中写入字节。

4个常用构造函数:

FileOutputStream(String filePath)

FileOutputStream(File fileObj)

FileOutputStream(String filePath, boolean append)

FileOutputStream(File fileObj, boolean append)

在开始下面的例子前,我们先提到流关闭的一个细节,那就是

在JDK 7之前的遗留代码,都使用显式地调用close()方法来关闭流,这种办法

比较笨拙。

老的代码:

try {      FileOutputStream f0 = new FileOutputStream("file1.txt");      // 文件写入操作      } catch(IOException e) {      System.out.println("An I/O Error Occurred");    } finally {      try {        if(f0 != null) f0.close();      } catch(IOException e) {        System.out.println("Error Closing file1.txt");      }    }}

新的代码(JDK 7及以后的代码):

带资源的try(try with resources)

try (FileOutputStream f0 = new FileOutputStream("file1.txt")) {  // 文件写入操作  } catch(IOException e) {  System.out.println("An I/O Error Occurred");}

 是不是代码变得更加简洁了?

 

我们的例子:

package corejava8.io.stream;import java.io.*;public class FileOutputStreamDemo {    public static void main(String args[]) {        String source = "Now is the time for all good men\n" + " to come to the aid of their country\n"                + " and pay their due taxes.";        byte buf[] = source.getBytes();        // Use try-with-resources to close the files.        try (FileOutputStream f0 = new FileOutputStream("file1.txt");                FileOutputStream f1 = new FileOutputStream("file2.txt");                FileOutputStream f2 = new FileOutputStream("file3.txt")) {            // write to first file            for (int i = 0; i < buf.length; i++)                f0.write(buf[i]);                        // write to second file            f1.write(buf);                        // write to third file            f2.write(buf, 3, buf.length - 3);        } catch (IOException e) {            System.out.println("An I/O Error Occurred");        }    }}

运行结果如下:

第一个,第二个文件内容都是:

Now is the time for all good men

to come to the aid of their country
and pay their due taxes.

第三个文件跳过第一个单词,内容如下:

is the time for all good men

to come to the aid of their country
and pay their due taxes.

上述代码中:

byte buf[] = source.getBytes();

用于获取源数据(字符串)的字节数组。

 

转载于:https://www.cnblogs.com/davidgu/p/6125850.html

你可能感兴趣的文章
十年再出发:阿里云智能战略加速的“四级火箭” ...
查看>>
阿里云RPA(机器人流程自动化)干货系列之六:客户端安装及激活 ...
查看>>
Alibaba Cluster Data 开放下载:270 GB 数据揭秘你不知道的阿里巴巴数据中心 ...
查看>>
Decision Tree
查看>>
四万高手过招,这份阿里全球数学竞赛试题你真的不要看吗 ...
查看>>
进行移动端Unity开发,针对引擎PowerVR性能建议
查看>>
linux gcc Makefile
查看>>
SpringBoot-Security-用户权限分配-配置验证规则
查看>>
学习大数据要掌握哪些语言?需要学习哪些内容? ...
查看>>
2019阿里云峰会-边缘计算专场,邀您共话大连接低时延场景下的技术探索与实践 ...
查看>>
云栖专辑| 阿里毕玄:程序员的成长路线
查看>>
jvm知识点总览
查看>>
如何在Windows 10 / 8.1 / 8中安装PHP或XAMP
查看>>
地铁译:Spark for python developers ---Spark的数据戏法
查看>>
举个栗子:专有云MaxCompute创建TableStore外部表
查看>>
网站服务器设置禁PING的方法步骤
查看>>
Linux基础命令---lpstat查看打印机状态
查看>>
新年福利 | 架构的“一小步”,业务的一大步
查看>>
Confluence 6 升级中的一些常见问题
查看>>
不可小看的数值类型—Python基础前传(5)
查看>>