Java教程

06 Java BIO java.io流;PushbackInputStream;SequenceInputStream;PrintStream;PushbackReader

本文主要是介绍06 Java BIO java.io流;PushbackInputStream;SequenceInputStream;PrintStream;PushbackReader,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

PushbackInputStream

PushbackInputStream用于解析InputStream内的数据。有时候你需要提前知道接下来将要读取到的字节内容,才能判断用何种方式进行数据解析。
PushBackInputStream允许你这么做,你可以把读取到的字节重新推回到InputStream中,以便再次通过read()读取

new PushbackInputStream(new FileInputStream(“D:/Sunxy_workspace/pushback.txt”), 8); 设置了8个字节的缓冲区,可以重新读取8个字节的数据

public class PushbackT {
    public static void main(String[] args) throws IOException {
        PushbackInputStream input = new PushbackInputStream(new FileInputStream("D:/Sunxy_workspace/pushback.txt"));
        int data = input.read();
        while (data != -1){
            char c =(char)data;
            System.out.println(c);
            if (c=='w'){
                input.unread(data);
                System.out.println((char)data);
                break;
            }
            data = input.read();
        }
    }
}

SequenceInputStream

SequenceInputStream把一个或者多个InputStream整合起来,形成一个逻辑连贯的输入流。

当读取SequenceInputStream时,会先从第一个输入流中读取,完成之后再从第二个输入流读取,以此推类

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.nio.charset.StandardCharsets;

public class SequenceStreamT {
    public static void main(String[] args) throws IOException {
        InputStream in1 = new ByteArrayInputStream("lingYiWin,".getBytes(StandardCharsets.UTF_8));
        InputStream in2 = new ByteArrayInputStream("EngineerForSoul".getBytes(StandardCharsets.UTF_8));
        InputStream in = new SequenceInputStream(in1, in2);
        int data = in.read();
        while (data != -1) {
            System.out.print((char) data);
            data = in.read();
        }
    }
}

PrintStream

PrintStream允许你把格式化数据写入到底层OutputStream中。
写入格式化成文本的int,long以及其他原始数据类型到输出流中,而非它们的字节数据。

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class PrintStreamT {
    public static void main(String[] args) throws FileNotFoundException {
        PrintStream output = new PrintStream(new FileOutputStream("D:/Sunxy_workspace/print.txt"));
        output.print(true);
        output.print((int) 123);
        output.print((float) 123.456);
        output.close();
    }
}

PushbackReader

PushbackReader与PushbackInputStream类似,唯一不同的是PushbackReader处理字符,PushbackInputStream处理字节

PushbackReader reader = new PushbackReader(new FileReader(file), 8); 设置缓冲区为8.

public class PushbackT {
    public static void main(String[] args) throws IOException {
        PushbackReader reader = new PushbackReader(new FileReader("D:/Sunxy_workspace/pushback2.txt"));
        int read = reader.read();
        while (read != -1){
            char c =(char)read;
            System.out.println(c);
            if (c=='天'){
                reader.unread(read);
                System.out.println((char)read);
                break;
            }
            read = reader.read();
        }
    }
}
这篇关于06 Java BIO java.io流;PushbackInputStream;SequenceInputStream;PrintStream;PushbackReader的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!