📜  Java Java类

📅  最后修改于: 2022-05-13 01:55:13.664000             🧑  作者: Mango

Java Java类

Java Java类

Java 中的 io.PipedOutputStream 类

IO 中的管道提供了同时在 JVM 中运行的两个线程之间的链接。因此,管道既可用作源,也可用作目标。

  • PipedInputStream 也使用 PipedOutputStream 进行管道传输。因此,可以使用 PipedOutputStream 写入数据,也可以使用 PipedInputStream 写入数据。但是,同时使用两个线程会为线程创建死锁。
  • PipedOutputStream 是管道的发送端。数据被写入 PipedOutputStream。如果正在读取数据的 PipedInputStream 不再存在,则表示管道已损坏。

宣言:

public class PipedOutputStream
  extends OutputStream

构造函数:

  • PipedOutputStream() :创建一个未连接的 PipedOutputStream。
  • PipedOutputStream(PipedOutputStream inStream) :创建一个 PipedOutputStream,它
    连接到 PipedInputStream – 'inStream'。

方法:

  • write() : Java.io.PipedOutputStream.write(int byte)将指定的字节写入管道输出流。
    句法 :
public void write(int byte)
Parameters : 
byte : byte to be written
Return :                                               
void
Exception :
-> IOException : if in case IO error occurs.
  • write(byte[] buffer, int offset, int maxlen) : Java.io.PipedOutputStream.write(byte[] buffer, int offset, int maxlen)将数据的 maxlen 字节从缓冲区写入管道输出流。如果没有字节写入 Stream,该方法将阻塞。
    句法 :
public void write(byte[] buffer, int offset, int maxlen)
Parameters : 
buffer : data of the buffer
offset : starting in the destination array - 'buffer'.
maxlen : maximum length of array to be read
Return :                                               
void
Exception :
-> IOException : if in case IO error occurs.
Java
// Java program illustrating the working of PipedInputStream
// write(byte[] buffer, int offset, int maxlen)
 
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        PipedInputStream geek_input = new PipedInputStream();
        PipedOutputStream geek_output = new PipedOutputStream();
 
        // Use of connect() : connecting geek_input with geek_output
        geek_input.connect(geek_output);
 
        byte[] buffer = {'J', 'A', 'V', 'A'};
 
        // Use of write(byte[] buffer, int offset, int maxlen)
        geek_output.write(buffer, 0, 4);
        int a = 5;
        System.out.print("Use of write(buffer, offset, maxlen) : ");
        while(a>0)
        {
            System.out.print(" " + (char) geek_input.read());
        }
    }
}


Java
// Java program illustrating the working of PipedInputStream
// write(), write(byte[] buffer, int offset, int maxlen),
// close(), flush(), connect()
 
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        PipedInputStream geek_input = new PipedInputStream();
        PipedOutputStream geek_output = new PipedOutputStream();
        try
        {
            // Use of connect() : connecting geek_input with geek_output
            geek_input.connect(geek_output);
 
            // Use of write(int byte) :
            geek_output.write(71);
            geek_output.write(69);
            geek_output.write(69);
            geek_output.write(75);
            geek_output.write(83);
 
            // Use of flush() method :
            geek_output.flush();
            System.out.println("Use of flush() method : ");
 
            int i = 5;
            while(i > 0)
            {
                System.out.print(" " + (char) geek_input.read());
                i--;
            }
 
            // USe of close() method :
            System.out.println("\nClosing the Output stream");
            geek_output.close();
 
        }
        catch (IOException except)
        {
            except.printStackTrace();
        }
    }
}


输出:

Use of write(buffer, offset, maxlen) :  J A V A
  • close() : Java.io.PipedOutputStream.close()关闭管道输出流并释放分配的资源。
    句法 :
public void close()
Parameters : 
--------------
Return :                                               
void
Exception :
-> IOException : if in case IO error occurs.
  • connect(PipedInputStream destination) : Java.io.PipedOutputStream.connect(PipedInputStream 目标将管道输出流连接到“目标”管道输入流,如果“目标”是带有其他流的管道,则会引发 IO 异常
    句法 :
public void connect(PipedInputStream destination)
Parameters : 
destination : the Piped Input Stream to be connected to
Return :                                               
void
Exception :
-> IOException : if in case IO error occurs.
  • flush() : Java.io.PipedOutputStream.flush()刷新输出流。
    句法 :
public void flush()
Parameters : 
------------
Return :                                               
void
Exception :
-> IOException : if in case IO error occurs.

说明 PipedOutputStream 类方法工作的Java代码:

Java

// Java program illustrating the working of PipedInputStream
// write(), write(byte[] buffer, int offset, int maxlen),
// close(), flush(), connect()
 
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        PipedInputStream geek_input = new PipedInputStream();
        PipedOutputStream geek_output = new PipedOutputStream();
        try
        {
            // Use of connect() : connecting geek_input with geek_output
            geek_input.connect(geek_output);
 
            // Use of write(int byte) :
            geek_output.write(71);
            geek_output.write(69);
            geek_output.write(69);
            geek_output.write(75);
            geek_output.write(83);
 
            // Use of flush() method :
            geek_output.flush();
            System.out.println("Use of flush() method : ");
 
            int i = 5;
            while(i > 0)
            {
                System.out.print(" " + (char) geek_input.read());
                i--;
            }
 
            // USe of close() method :
            System.out.println("\nClosing the Output stream");
            geek_output.close();
 
        }
        catch (IOException except)
        {
            except.printStackTrace();
        }
    }
}

输出:

Use of flush() method : 
 G E E K S
Closing the Output stream