Java-I/O學習(5)

Java-I/O學習(5)

ByteArrayInputStream

ByteArrayInputStream類可以讓你從一個字節數組來讀取流

創建


//Creates a ByteArrayInputStream so that it uses buf as its buffer array. The buffer array is not copied. The initial value of pos is 0 and the initial value of count is the length of buf.
ByteArrayInputStream​(byte[] buf)


//Creates ByteArrayInputStream that uses buf as its buffer array. The initial value of pos is offset and the initial value of count is the minimum of offset+length and buf.length. The buffer array is not copied. The buffer's mark is set to the specified offset.
ByteArrayInputStream​(byte[] buf,int offset,int length)

使用:


String s = "abcdefghijlmnopq";
        
        
ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());

read


//Reads the next byte of data from this 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 read method cannot block.

//Returns:
//the next byte of data, or -1 if the end of the stream has been reached.
int read()


//Reads up to len bytes of data into an array of bytes from this input stream. If pos equals count, then -1 is returned to indicate end of file. Otherwise, the number k of bytes read is equal to the smaller of len and count-pos. If k is positive, then bytes buf[pos] through buf[pos+k-1] are copied into b[off] through b[off+k-1] in the manner performed by System.arraycopy. The value k is added into pos and k is returned.
//This read method cannot block.

//Returns:
//the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
int read​(byte[] b,int off,int len)

使用:


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

        String s = "abcdefghijlmnopq";

        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());

        int data = in.read();

        while (data != -1) {

            System.out.println((char) data);
            data = in.read();
        }

        in.close();

    }

其它


//Skips n bytes of input from this input stream. Fewer bytes might be skipped if the end of the input stream is reached. The actual number k of bytes to be skipped is equal to the smaller of n and count-pos. The value k is added into pos and k is returned.
long skip​(long n)

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

        String s = "abcdefghijlmnopq";

        ByteArrayInputStream in = new ByteArrayInputStream(s.getBytes());

        in.skip(5);

        int data = in.read();

        while (data != -1) {

            System.out.println((char) data);
            data = in.read();
        }

        in.close();

    }

ByteArrayOutputStream

Java IO API的ByteArrayOutputStream類允許您捕獲寫入到一個數組中的流的數據。你把數據寫到ByteArrayOutputStream,寫完之後,調用toByteArray()方法就可以以字節數組的形式獲得所有的已寫的數據。

創建


//Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary.
ByteArrayOutputStream()


//Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
ByteArrayOutputStream​(int size)


write


//Writes the specified byte to this byte array output stream.
//Parameters:
//b - the byte to be written
void write​(int b)

//Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
void write​(byte[] b,int off,int len)


//Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).
void writeTo​(OutputStream out)

其餘方法



//Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.
byte[] toByteArray()

//Returns the current size of the buffer.
int size()

使用:


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

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        String s = "1234567890";
        out.write(60);
        out.write(s.getBytes());
        for (byte b : out.toByteArray()){
            System.out.println((char)b);
        }

    }

BufferedInputStream

BufferedInputStream爲你的輸入流提供了一個緩衝區。緩衝區可以大大的提高IO速度。不是每次從網絡或磁盤上讀取一個字節,而是每次讀取一大塊兒內容到內部的緩衝區中。當你從BufferedInputStream讀取一個字節時,你其實是從它內部的緩衝區讀取的。當緩衝區已經讀完,BufferedInputStream會讀另一大塊的數據到緩衝區中。這通常要比每次讀取單字節要快的多,尤其是訪問磁盤和大數據量的情況。

創建


//Creates a BufferedInputStream and saves its argument, the input stream in, for later use. An internal buffer array is created and stored in buf.
BufferedInputStream​(InputStream in)

//Creates a BufferedInputStream with the specified buffer size, and saves its argument, the input stream in, for later use. An internal buffer array of length size is created and stored in buf.
BufferedInputStream​(InputStream in,int size)
  public static void main(String[] args) throws FileNotFoundException {

        String path = "E:\\learn-java\\Learning-Java\\MyNotes\\better_write\\Java_IO\\c.txt";

        FileInputStream inputStream = new FileInputStream("");

        BufferedInputStream in = new BufferedInputStream(inputStream);
        
    }

read

//See the general contract of the read method of InputStream.
//Returns:
//the next byte of data, or -1 if the end of the stream is reached.
int read()

//Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.
int read​(byte[] b,int off,int len)

   String path = "E:\\learn-java\\Learning-Java\\MyNotes\\better_write\\Java_IO\\c.txt";

        FileInputStream inputStream = new FileInputStream(path);

        BufferedInputStream in = new BufferedInputStream(inputStream);

        int data = in.read();

        while (data != -1) {

            System.out.println(data);

            data = in.read();
        }
        in.close();

BufferedOutputStream

BufferedOutputStream 爲你的輸出流提供了一個緩衝區。緩衝區可以大大的提高IO速度。不是每次從網絡或磁盤上讀取一個字節,而是每次讀取一大塊兒內容到內部的緩衝區中。這通常要比每次讀取單字節要快的多,尤其是訪問磁盤和大數據量的情況。

創建


//Creates a new buffered output stream to write data to the specified underlying output stream.
BufferedOutputStream​(OutputStream out)

//Creates a new buffered output stream to write data to the specified underlying output stream with the specified buffer size.
BufferedOutputStream​(OutputStream out,int size)

write


//Writes the specified byte to this buffered output stream.
void write​(int b)


//Writes len bytes from the specified byte array starting at offset off to this buffered output stream.
void write​(byte[] b,int off,int len)


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


        String path = "E:\\learn-java\\Learning-Java\\MyNotes\\better_write\\Java_IO\\t1.txt";

        FileOutputStream outputStream = new FileOutputStream(path,true);

        BufferedOutputStream out = new BufferedOutputStream(outputStream);

        out.write("where are you from".getBytes());

        out.close();

    }


PushbackInputStream

在JAVA IO中所有的數據都是採用順序的讀取方式,即對於一個輸入流來講都是採用從頭到尾的順序讀取的,如果在輸入流中某個不需要的內容被讀取進來,則只能通過程序將這些不需要的內容處理掉,爲了解決這樣的處理問題,在JAVA中提供了一種回退輸入流(PushbackInputStream、PushbackReader),可以把讀取進來的某些數據重新回退到輸入流的緩衝區之中。

創建


//Creates a PushbackInputStream with a 1-byte pushback buffer, and saves its argument, the input stream in, for later use. Initially, the pushback buffer is empty.
PushbackInputStream​(InputStream in)


//Creates a PushbackInputStream with a pushback buffer of the specified size, and saves its argument, the input stream in, for later use. Initially, the pushback buffer is empty.
PushbackInputStream​(InputStream in,int size)

unread


//Pushes back a byte by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value (byte)b.
void unread​(int b)

//Pushes back a portion of an array of bytes by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value b[off], the byte after that will have the value b[off+1], and so forth.
void unread​(byte[] b,int off,int len)

//Pushes back an array of bytes by copying it to the front of the pushback buffer. After this method returns, the next byte to be read will have the value b[0], the byte after that will have the value b[1], and so forth.
void unread​(byte[] b)

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

        String path = "E:\\learn-java\\Learning-Java\\MyNotes\\better_write\\Java_IO\\t1.txt";

        FileInputStream inputStream = new FileInputStream(path);

        PushbackInputStream in = new PushbackInputStream(inputStream);

        int data = 0;

        while ((data = in.read()) != -1) {

            if ((char)data == '5'){

                in.unread(data);
                data = in.read();
//                System.out.println("unread " +(char)data);
            } else {
                System.out.println((char)data);
            }

        }
    }

SequenceInputStream

SequenceInputStream可以將兩個或更多個InputStream合併成一個。它會首先讀取第一個的全部字節,然後第二個。這就是叫SequenceInputStream的原因,因爲它是安順序讀取的。

創建


//Initializes a newly created SequenceInputStream by remembering the two arguments, which will be read in order, first s1 and then s2, to provide the bytes to be read from this SequenceInputStream.
SequenceInputStream​(InputStream s1,InputStream s2)

//Initializes a newly created SequenceInputStream by remembering the argument, which must be an Enumeration that produces objects whose run-time type is InputStream. The input streams that are produced by the enumeration will be read, in order, to provide the bytes to be read from this SequenceInputStream. After each input stream from the enumeration is exhausted, it is closed by calling its close method.
SequenceInputStream​(Enumeration<? extends InputStream> e)


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


        String path1 = "E:\\learn-java\\Learning-Java\\MyNotes\\better_write\\Java_IO\\t1.txt";

        String path2 = "E:\\learn-java\\Learning-Java\\MyNotes\\better_write\\Java_IO\\t2.txt";

        FileInputStream f1 = new FileInputStream(path1);

        FileInputStream f2 = new FileInputStream(path2);

        SequenceInputStream in = new SequenceInputStream(f1,f2);

        int data = 0;

        while ((data = in.read()) != -1) {

            System.out.println((char) data);

        }

        in.close();

    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章