java .io OutputStream 与InputStream

 outputStream首先声明这是一个抽象类,所以关于输出的类都继承与这个类。

三个基本的写方法
abstract void write(int b) :往输出流中写入指定的字节。
void write(byte[] b) :往输出流中写入数组b中的所有字节。
void write(byte[] b, int off, int len) :往输出流中写入数组b中从偏移量off开始的len个字节的数据

记住write的操作都是对byte 数据操作

Output streams exist to allow data to be written to some data consumer; what sort ofconsumer is unimportant because the output stream objects define methods that allow data to be sent to any sort of data consumer。

注释:Even though the write methods specify that they accept ints, they are actually accepting bytes. Only the lower 8 bytes of the int are actually used.

byte b = new byte[100]; // creates a byte array
output.write( b ); // writes the byte array

以上的output为OutputStream类型,写入数据到b中。

If an output stream is an abstract class, where does it come from? How do you instantiate an OutputStream class? OutputStream objects are never obtained directly by using the new operator. Rather, OutputStream objects are usually obtained from other objects. For example, the Socket class contains a method called getOutputStream. Calling the  getOutputStream method will return an OutputStream object that will be used to write to the socket. Other output streams are obtained by different means.

关闭流public void close()

OutputStream对象一般由其他类的方法来得到

public class InStream {
    public static void main(String[] args) throws FileNotFoundException
    {
        OutputStream os=new FileOutputStream("C:\\read.txt",true);//true 表示可以从文件后追加文章
        String str="welcome";
        byte[] buffer=str.getBytes();//将字符串转成byte 类型
        try {
            os.write(buffer);
            os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("done!");        
    }

}

read.txt如果不存在,会创建一个新的。

InputStream

The InputStream class provided by Java is abstract, and it is only meant to be overridden to provide InputStream classes for such things as socket- and disk-based input. The InputStream provided by Java provides the following methods:

public abstract int read()  Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

public int read(byte[] b)

public int read(byte[] b, int off, int len)

 public void close()

Any I/O operation can be accomplished with the InputStream and OutputStream classes. These classes are like atoms: you can build anything with them, but they are very basic building blocks. The InputStream and OutputStream classes only give you access to the raw bytes of the connection. It’s up to you to determine whether the underlying meaning of these bytes is a string, an IEEE754 floating point number, Unicode text, or some other binary construct.

Filters are generally used as a sort of attachment to the InputStream and OutputStream classes to hide the low-level complexity of working solely with bytes. There are two primary types of filters. The first is the basic filter, which is used to transform the underlying binary numbers into meaningful data types. Many different basic filters have
been created; there are filters to compress, encrypt, and perform various translations on data.


 Read Filter                  Write Filter                             

BufferedInputStream  BufferedOutputStream

DataInputStream   DataOutputStream

GZIPInputStream  GZIPOutputStream

等等的过滤器。

The second type of filter is really a set of filters that work together; the filters that compose  this set are called readers and writers.

Filters themselves are extended from the FilterInputStream and  FilterOutputStream classes。即4这些filter都是继承了FilterInputS和FilterOutputStream类。而这两个类又继承了java.io.InputStream 和OutputStream这2个类。正因为如此,有read和write方法。

Chaining Filters Together

One very important feature of filters is their ability to chain themselves together. A basic filter can be layered on top of either an input/output stream or another filter. A reader/writer can be layered on top of an input/output stream or another filter but never on another reader/ writer.Readers and writers must always be the last filter in a chain.

reader write 总是链条的最后一个。
Filters are layered by passing the underlying filter or stream into the constructor of the new
stream. For example, to open a file with a BufferedInputStream, the following code should be used:

FileInputStream fin = new FileInputStream("myfile.txt");
BufferedInputStream bis = new BufferedInputStream(fin);

 其实BufferedInputStream的参数类型为InputStream ,我们这里的是FileInputStream类型,由于基类是InputStream,所以这里有一个隐士转换

FileInputStream 与InputStream 关系

    InputStream不可以读取文件,它是一个Abstract的类,根本不可能实例化,是所有输入流的基类。而FileInputStream是InputStream的一个实现类,用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader 。

java.lang.Object
     java.io.InputStream    
            java.io.FilterInputStream  
                    java.io.BufferedInputStream (BufferedInputStream与InputStream有一个FilterInputStream

java.lang.Object
        java.io.InputStream
              java.io.FileInputStream

 

 

原文地址:https://www.cnblogs.com/youxin/p/2601646.html