Core Java - 流(Stream) - 字节流和字符流(一)

简介: 0. 概述: Java中基于流的I/O构建在4个抽象类之上, 其中2个是字节流,另外2个是字符流。 字节流: InputStream / OutputStream 当操作字节或其它二进制对象时,应当使用字节流。

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();

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

 

目录
相关文章
|
3月前
|
Java API 数据处理
Java新特性:使用Stream API重构你的数据处理
Java新特性:使用Stream API重构你的数据处理
|
3月前
|
Java 大数据 API
Java Stream API:现代集合处理与函数式编程
Java Stream API:现代集合处理与函数式编程
242 100
|
3月前
|
Java API 数据处理
Java Stream API:现代集合处理新方式
Java Stream API:现代集合处理新方式
275 101
|
3月前
|
并行计算 Java 大数据
Java Stream API:现代数据处理之道
Java Stream API:现代数据处理之道
245 101
|
4月前
|
存储 Java API
Java Stream API:现代数据处理之道
Java Stream API:现代数据处理之道
369 188
|
4月前
|
存储 Java API
Java Stream API:现代数据处理之道
Java Stream API:现代数据处理之道
281 92
|
2月前
|
Java Unix Go
【Java】(8)Stream流、文件File相关操作,IO的含义与运用
Java 为 I/O 提供了强大的而灵活的支持,使其更广泛地应用到文件传输和网络编程中。!但本节讲述最基本的和流与 I/O 相关的功能。我们将通过一个个例子来学习这些功能。
177 2
|
5月前
|
Oracle Java 关系型数据库
掌握Java Stream API:高效集合处理的利器
掌握Java Stream API:高效集合处理的利器
396 80
|
5月前
|
安全 Java API
Java 8 Stream API:高效集合处理的利器
Java 8 Stream API:高效集合处理的利器
292 83
|
3月前
|
存储 数据可视化 Java
Java Stream API 的强大功能
Java Stream API 是 Java 8 引入的重要特性,它改变了集合数据的处理方式。通过声明式语法,开发者可以更简洁地进行过滤、映射、聚合等操作。Stream API 支持惰性求值和并行处理,提升了代码效率和可读性,是现代 Java 开发不可或缺的工具。
Java Stream API 的强大功能