openplanning

Hướng dẫn và ví dụ Java SequenceInputStream

Xem thêm các chuyên mục:

Nhóm phát triển của chúng tôi vừa ra mắt website langlearning.net học tiếng Anh, Nga, Đức, Pháp, Việt, Trung, Hàn, Nhật, ... miễn phí cho tất cả mọi người.
Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi.
Hãy theo dõi chúng tôi trên Fanpage để nhận được thông báo mỗi khi có bài viết mới. Facebook

1- SequenceInputStream

SequenceInputStream cho phép bạn nối (concatenate) hai hoặc nhiều InputStream với nhau. Nó đọc từ byte đầu tiên cho tới byte cuối cùng của InputStream đầu tiên, sau đó làm tương tự với InputStream tiếp theo cho tới InputStream cuối cùng.
SequenceInputStream là một lớp con của InputStream:
SequenceInputStream​ Methods

final void nextStream() throws IOException  

// Methods inherited from InputStream

public int available() throws IOException  
public int read() throws IOException  
public int read(byte b[], int off, int len) throws IOException  
public int read(byte[] b) throws IOException  
public byte[] readAllBytes() throws IOException  
public byte[] readNBytes(int len) throws IOException  
public int readNBytes(byte[] b, int off, int len) throws IOException  

public long skip(long n) throws IOException

public boolean markSupported()
public synchronized void mark(int readlimit)

public synchronized void reset() throws IOException  
public void close() throws IOException

public long transferTo(OutputStream out) throws IOException
Hầu hết các phương thức của SequenceInputStream được thừa kế từ InputStream, vì vậy bạn có thể tìm hiểu cách sử dụng các phương thức này trong bài viết về InputStream:
SequenceInputStream​ Constructors

public SequenceInputStream​(InputStream s1, InputStream s2)    

public SequenceInputStream​(Enumeration<? extends InputStream> e)

2- Examples

Giả sử chúng ta có 2 file văn bản mã hoá UTF-8, mỗi file chứa danh sách tên các loài hoa.
flowers-1.txt

# Flower names (1)

Tulip
Daffodil
Poppy
Sunflower
Bluebell
Rose
Snowdrop
Cherry blossom
flowers-2.txt

# Flower names (2)

Orchid
Iris
Peony
Chrysanthemum
Geranium
Lily
Lotus
Water lily
Dandelion
Hyacinth
Daisy
Crocus
Chúng ta sẽ đọc 2 file nói trên bằng cách sử dụng SequenceInputStream, InputStreamReaderBufferedReader.
SequenceInputStreamEx1.java

package org.o7planning.sequenceinputstream.ex;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.SequenceInputStream;

public class SequenceInputStreamEx1 {

    // Windows: C:/Data/test/flowers-1.txt
    private static String file_path1 = "/Volumes/Data/test/flowers-1.txt";
    private static String file_path2 = "/Volumes/Data/test/flowers-2.txt";

    public static void main(String[] args) throws IOException {

        InputStream is1 = new FileInputStream(file_path1);
        InputStream is2 = new FileInputStream(file_path2);
        
        // Create SequenceInputStream:
        InputStream is = new SequenceInputStream(is1, is2);
        
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        
        String line;
        while((line = br.readLine())!= null)  {
            System.out.println(line);
        }
        br.close();
    }
}
Output:

# Flower names (1)

Tulip
Daffodil
Poppy
Sunflower
Bluebell
Rose
Snowdrop
Cherry blossom
# Flower names (2)

Orchid
Iris
Peony
Chrysanthemum
Geranium
Lily
Lotus
Water lily
Dandelion
Hyacinth
Daisy
Crocus
Cải tiến ví dụ trên, chỉ in ra các dòng không rỗng và không phải dòng chú thích (Bắt đầu với "#").
SequenceInputStreamEx2.java

package org.o7planning.sequenceinputstream.ex;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.SequenceInputStream;

public class SequenceInputStreamEx2 {

    // Windows: C:/Data/test/flowers-1.txt
    private static String file_path1 = "/Volumes/Data/test/flowers-1.txt";
    private static String file_path2 = "/Volumes/Data/test/flowers-2.txt";

    public static void main(String[] args) throws IOException {

        InputStream is1 = new FileInputStream(file_path1);
        InputStream is2 = new FileInputStream(file_path2);
        
        // Create SequenceInputStream:
        InputStream is = new SequenceInputStream(is1, is2);
        
        InputStreamReader isr = new InputStreamReader(is, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        
        br.lines() // Stream
           .filter(line -> !line.isBlank()) // Not blank
           .filter(line -> !line.startsWith("#")) // Not start with "#"
           .forEach(System.out::println);
        
        br.close();
    }
}
Output:

Tulip
Daffodil
Poppy
Sunflower
Bluebell
Rose
Snowdrop
Cherry blossom
Orchid
Iris
Peony
Chrysanthemum
Geranium
Lily
Lotus
Water lily
Dandelion
Hyacinth
Daisy
Crocus

Xem thêm các chuyên mục: