原文:http://zetcode.com/java/inputstream/

Java InputStream教程显示了如何使用 Java 中的InputStream类。

Java 流是来自源或目的地的数据流。 Java 流的一个很好的比喻是水从水龙头流入浴缸,然后又流入排水装置。 InputStreamOutputStream是对数据(例如 C 文件指针)的低级访问的抽象。

Java InputStream

InputStream是读取数据的源。 流可以表示各种类型的源,包括磁盘文件,设备,其他程序和内存数组。

流支持许多不同类型的数据,包括简单字节,原始数据类型,本地化字符和对象。

Java InputStream子类

InputStream是一个抽象类; 它是表示字节输入流的所有类的超类,包括AudioInputStreamByteArrayInputStreamFileInputStreamFilterInputStreamObjectInputStreamPipedInputStreamSequenceInputStream

Java InputStream关闭

FileInputStreamclose()方法关闭输入流,并释放与此流关联的所有系统资源。 在我们的示例中,我们使用try-with-resources语句,该语句确保在语句末尾关闭每个资源。

Java InputStream读取

InputStream使用以下读取方法读取字节:

  • read(byte[] b) - 从此输入流中读取最多b.length个字节的数据到一个字节数组中。
  • read(byte[] b, int off, int len) - 从此输入流中读取最多len个字节的数据到一个字节数组中。
  • read() - 从文件输入流中读取一个字节。

Java InputStream读取文本

以下示例显示如何使用InputStream读取文本文件。

thermopylae.txt

  1. The Battle of Thermopylae was fought between an alliance of Greek city-states,
  2. led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the
  3. course of three days, during the second Persian invasion of Greece.

在示例中,我们使用此文本文件。

JavaInputStreamText.java

  1. package com.zetcode;
  2. import java.io.BufferedReader;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.nio.charset.StandardCharsets;
  8. public class JavaInputStreamText {
  9. public static void main(String[] args) throws IOException {
  10. String fileName = "src/resources/thermopylae.txt";
  11. try (InputStream fis = new FileInputStream(fileName);
  12. InputStreamReader isr = new InputStreamReader(fis,
  13. StandardCharsets.UTF_8);
  14. BufferedReader br = new BufferedReader(isr)) {
  15. br.lines().forEach(line -> System.out.println(line));
  16. }
  17. }
  18. }

使用FileInputStreamInputStreamReaderBufferedReader读取文本文件。

  1. try (InputStream fis = new FileInputStream(fileName);

FileInputStreamInputStream的一种特殊形式,用于从文件中读取字节。

  1. InputStreamReader isr = new InputStreamReader(fis,
  2. StandardCharsets.UTF_8);

InputStreamReader是从字节流到字符流的桥梁:它读取字节,并使用指定的字符集将其解码为字符。

  1. BufferedReader br = new BufferedReader(isr)) {

BufferedReader从字符输入流中读取文本,缓冲字符以有效读取字符,数组和行。

  1. br.lines().forEach(line -> System.out.println(line));

从缓冲读取器中按行读取数据。

Java InputStream读取字节

InputStream的读取方法读取字节。

JavaInputStreamBytes.java

  1. package com.zetcode;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. public class JavaInputStreamBytes {
  6. public static void main(String[] args) throws IOException {
  7. String fileName = "src/resources/ball.png";
  8. try (InputStream is = new FileInputStream(fileName)) {
  9. byte[] buffer = new byte[is.available()];
  10. is.read(buffer);
  11. int i = 0;
  12. for (byte b: buffer) {
  13. if (i % 10 == 0) {
  14. System.out.println();
  15. }
  16. System.out.printf("%02x ", b);
  17. i++;
  18. }
  19. }
  20. System.out.println();
  21. }
  22. }

该示例从 PNG 图像读取字节,并将字节以十六进制格式打印到控制台。

  1. try (InputStream is = new FileInputStream(fileName)) {

我们使用FileInputStream从图像文件中读取字节。

  1. byte[] buffer = new byte[is.available()];
  2. is.read(buffer);

使用read()方法,我们将字节读入字节数组。

  1. int i = 0;
  2. for (byte b: buffer) {
  3. if (i % 10 == 0) {
  4. System.out.println();
  5. }
  6. System.out.printf("%02x ", b);
  7. i++;
  8. }

我们遍历数组并将字节以十六进制格式打印到控制台。

  1. 89 50 4e 47 0d 0a 1a 0a 00 00
  2. 00 0d 49 48 44 52 00 00 00 0a
  3. 00 00 00 0a 08 06 00 00 00 8d
  4. 32 cf bd 00 00 00 04 73 42 49
  5. 54 08 08 08 08 7c 08 64 88 00
  6. 00 00 09 70 48 59 73 00 00 0d
  7. d7 00 00 0d d7 01 42 28 9b 78
  8. 00 00 00 19 74 45 58 74 53 6f
  9. ...

这是示例的部分示例输出。

从 URL 读取 Java InputStream

InputStream允许从 URL 源读取数据。

JavaInputStreamURL.java

  1. package com.zetcode;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.URL;
  7. public class JavaInputStreamURL {
  8. public static void main(String[] args) throws IOException {
  9. String webSite = "http://www.something.com";
  10. URL url = new URL(webSite);
  11. try (InputStream is = url.openStream();
  12. BufferedReader br = new BufferedReader(
  13. new InputStreamReader(is))) {
  14. br.lines().forEach(System.out::println);
  15. }
  16. }
  17. }

该示例将InputStream打开到网页并读取其数据。

  1. try (InputStream is = url.openStream();

使用openStream()方法创建 URL 的InputStream

  1. <html><head><title>Something.</title></head>
  2. <body>Something.</body>
  3. </html>

这是输出。

Java InputStream读取反序列化的数据

ObjectInputStream读取先前使用ObjectOutputStream写入的序列化数据。

Country.java

  1. package com.zetcode;
  2. import java.io.Serializable;
  3. public class Country implements Serializable {
  4. static final long serialVersionUID = 42L;
  5. private String name;
  6. private int population;
  7. public Country(String name, int population) {
  8. this.name = name;
  9. this.population = population;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getPopulation() {
  18. return population;
  19. }
  20. public void setPopulation(int population) {
  21. this.population = population;
  22. }
  23. }

这是Country bean。 我们将序列化和反序列化国家列表。

JavaObjectOutputStreamEx.java

  1. package com.zetcode;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.ObjectOutputStream;
  5. import java.io.OutputStream;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. public class JavaObjectOutputStreamEx {
  9. public static void main(String[] args) throws IOException {
  10. String fileName = "src/resources/myfile.dat";
  11. try (OutputStream fis = new FileOutputStream(fileName);
  12. ObjectOutputStream out = new ObjectOutputStream(fis)) {
  13. List<Country> countries = new ArrayList<>();
  14. countries.add(new Country("Slovakia", 5429000));
  15. countries.add(new Country("Norway", 5271000));
  16. countries.add(new Country("Croatia", 4225000));
  17. countries.add(new Country("Russia", 143439000));
  18. out.writeObject(countries);
  19. }
  20. }
  21. }

该示例序列化对象列表。

  1. out.writeObject(countries);

国家列表被写入ObjectOutputStream

JavaInputStreamObjects.java

  1. package com.zetcode;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.ObjectInputStream;
  6. import java.util.List;
  7. public class JavaInputStreamObjects {
  8. public static void main(String[] args) throws IOException, ClassNotFoundException {
  9. String fileName = "src/resources/myfile.dat";
  10. try (InputStream fis = new FileInputStream(fileName);
  11. ObjectInputStream oin = new ObjectInputStream(fis)) {
  12. List<Country> countries = (List<Country>) oin.readObject();
  13. countries.forEach(System.out::println);
  14. }
  15. }
  16. }

我们使用ObjectInputStream读取序列化的数据。

Java InputStream读取流序列

SequenceInputStream代表一系列输入流。 它允许从多个有序流中读取。

JavaInputStreamSequence.java

  1. package com.zetcode;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.SequenceInputStream;
  6. public class JavaInputStreamSequence {
  7. public static void main(String[] args) throws IOException {
  8. String fileName1 = "src/resources/myfile.txt";
  9. String fileName2 = "src/resources/myfile1.txt";
  10. String fileName3 = "src/resources/myfile2.txt";
  11. try (InputStream is1 = new FileInputStream(fileName1);
  12. InputStream is2 = new FileInputStream(fileName2);
  13. InputStream is3 = new FileInputStream(fileName3);
  14. SequenceInputStream sis1 = new SequenceInputStream(is1, is2);
  15. SequenceInputStream sis = new SequenceInputStream(sis1, is3)) {
  16. int b = sis.read();
  17. while (b != -1) {
  18. System.out.printf("%c", b);
  19. b = sis.read();
  20. }
  21. System.out.println();
  22. }
  23. }
  24. }

该示例从三个FileInputStreams中读取。

  1. try (InputStream is1 = new FileInputStream(fileName1);
  2. InputStream is2 = new FileInputStream(fileName2);
  3. InputStream is3 = new FileInputStream(fileName3);
  4. SequenceInputStream sis1 = new SequenceInputStream(is1, is2);
  5. SequenceInputStream sis = new SequenceInputStream(sis1, is3)) {

我们定义了三个输入流,并将这些流放入SequenceInputStreams中。

  1. int b = sis.read();
  2. while (b != -1) {
  3. System.out.printf("%c", b);
  4. b = sis.read();
  5. }

我们使用read()从流中读取数据。

在本教程中,我们介绍了 Java InputStream类。 您可能也对相关教程感兴趣: Java InputStreamReader教程Java FileOutputStream教程Java FileInputStream教程Java 文件时间Java FileWriter教程Java 附加到文件用 Java 读取文本文件Java 教程