Java-I/O学习(5)

作者: Cool_Pomelo | 来源:发表于2020-04-04 10:01 被阅读0次

    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();
    
        }
    
    

    相关文章

      网友评论

        本文标题:Java-I/O学习(5)

        本文链接:https://www.haomeiwen.com/subject/wlsdphtx.html