杰瑞科技汇

Java openstream如何正确使用与异常处理?

Of course! In Java, "open stream" is a general term that refers to the process of getting an InputStream or OutputStream to read from or write to a source. The source can be a file, a network connection, a byte array, or even a string.

Java openstream如何正确使用与异常处理?-图1
(图片来源网络,侵删)

Let's break down the most common ways to open streams, categorized by the type of stream and the data source.


Reading from a File (InputStream)

This is the most frequent use case. You want to read the contents of a file into your Java program.

The Classic Way: FileInputStream

This is the most direct way to open a stream from a file. It's simple but has limitations, like not handling character encodings properly (it's for raw bytes).

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FileInputStreamExample {
    public static void main(String[] args) {
        String filePath = "my-file.txt";
        // The try-with-resources statement ensures the stream is closed automatically.
        try (InputStream inputStream = new FileInputStream(filePath)) {
            int byteData;
            System.out.println("Reading file byte by byte:");
            // read() returns an int representing the byte, or -1 if the end of the stream is reached.
            while ((byteData = inputStream.read()) != -1) {
                System.out.print((char) byteData); // Cast to char to display it
            }
            System.out.println("\nFile read successfully.");
        } catch (IOException e) {
            System.err.println("An error occurred while reading the file: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Key Points:

Java openstream如何正确使用与异常处理?-图2
(图片来源网络,侵删)
  • try-with-resources: This is the modern, recommended way to handle resources like streams. It automatically calls the close() method when the block is exited, even if an exception occurs. This prevents resource leaks.
  • inputStream.read(): This method reads one byte of data from the stream.
  • Casting to char: FileInputStream reads raw bytes. If your file contains text, you need to know the encoding to convert these bytes into characters. This is where InputStreamReader and BufferedReader are better.

The Modern & Recommended Way: Files.newInputStream()

Since Java 7, the java.nio.file package provides a more robust and flexible API. Files.newInputStream() is the preferred method for opening a file stream.

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ModernFileInputStreamExample {
    public static void main(String[] args) {
        Path path = Paths.get("my-file.txt");
        try (InputStream inputStream = Files.newInputStream(path)) {
            // The rest of the code is the same as before
            int byteData;
            System.out.println("Reading file byte by byte:");
            while ((byteData = inputStream.read()) != -1) {
                System.out.print((char) byteData);
            }
            System.out.println("\nFile read successfully.");
        } catch (IOException e) {
            System.err.println("An error occurred: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Why is this better?

  • It's part of the modern java.nio.file API, which is generally more powerful.
  • It integrates seamlessly with other Files utility methods.
  • It can take advantage of Path objects, which are more flexible than simple String file paths.

Reading Text from a File (The Right Way)

If you are reading a text file, you should chain streams to handle character encoding and buffering efficiently.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class TextFileReaderExample {
    public static void main(String[] args) {
        Path path = Paths.get("my-file.txt");
        // 1. Open a byte stream to the file
        // 2. Wrap it in an InputStreamReader to convert bytes to characters (specifies encoding)
        // 3. Wrap that in a BufferedReader for efficient line-by-line reading
        try (BufferedReader reader = new BufferedReader(
                 new InputStreamReader(Files.newInputStream(path)))) {
            System.out.println("Reading file line by line:");
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            System.err.println("An error occurred: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Why this chain?

Java openstream如何正确使用与异常处理?-图3
(图片来源网络,侵删)
  • Files.newInputStream(path): Gets the raw bytes.
  • InputStreamReader: Acts as a bridge, converting the byte stream into a character stream using a specific character set (e.g., UTF-8, ISO-8859-1). If you don't specify a charset, it uses the system's default, which can cause problems on different machines.
  • BufferedReader: Reads text from a character-input stream, buffering characters for efficient reading of lines, arrays, and characters. This is much faster than reading character by character.

Writing to a File (OutputStream)

The principles are the same, but you use OutputStream classes.

Classic: FileOutputStream

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class FileOutputStreamExample {
    public static void main(String[] args) {
        String filePath = "output.txt";
        String content = "Hello, Java Streams!";
        try (OutputStream outputStream = new FileOutputStream(filePath)) {
            // Convert the string to bytes and write it to the stream
            outputStream.write(content.getBytes());
            System.out.println("Content written to file successfully.");
        } catch (IOException e) {
            System.err.println("An error occurred while writing to the file: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Modern: Files.newOutputStream()

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ModernFileOutputStreamExample {
    public static void main(String[] args) {
        Path path = Paths.get("output.txt");
        String content = "Hello, Modern Java!";
        try (OutputStream outputStream = Files.newOutputStream(path)) {
            outputStream.write(content.getBytes());
            System.out.println("Content written to file successfully.");
        } catch (IOException e) {
            System.err.println("An error occurred: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

Other Common Stream Sources

Streams aren't just for files.

Source Reading (InputStream) Writing (OutputStream)
Byte Array new ByteArrayInputStream(byte[] array) new ByteArrayOutputStream()
String new ByteArrayInputStream(str.getBytes()) N/A (use String.getBytes() or a StringWriter)
Network URL url.openStream() url.openConnection().getOutputStream()
Process Output process.getInputStream() process.getOutputStream()

Example: Reading from a String

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class StringStreamExample {
    public static void main(String[] args) {
        String data = "This is a string in a stream.";
        byte[] byteArray = data.getBytes();
        // Create an InputStream from the byte array
        try (InputStream inputStream = new ByteArrayInputStream(byteArray)) {
            int byteRead;
            while ((byteRead = inputStream.read()) != -1) {
                System.out.print((char) byteRead);
            }
        } catch (IOException e) {
            // This exception is unlikely for a ByteArrayInputStream
            e.printStackTrace();
        }
    }
}

Summary & Best Practices

  1. Use try-with-resources: Always wrap your streams in a try-with-resources block to ensure they are closed.
  2. Prefer java.nio.file.Files: For file I/O, Files.newInputStream() and Files.newOutputStream() are the modern, recommended choices.
  3. Chain for Text: When reading text, chain InputStream -> InputStreamReader -> BufferedReader. This handles encoding and provides efficient reading.
  4. Know Your Data: Use FileInputStream/FileOutputStream for raw binary data (images, videos, etc.). Use the text-chain method for .txt, .csv, .json, etc.
分享:
扫描分享到社交APP
上一篇
下一篇