The transferTo() method has been added to the InputStream class in Java 9. This method has been used to copy data from input streams to output streams in Java. It means it reads all bytes from an input stream and writes the bytes to an output stream in the order in which they are reading.
Syntax
public long transferTo(OutputStream out) throws IOException
Example
import java.util.Arrays; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class TransferToMethodTest { public void testTransferTo() throws IOException { byte[] inBytes = "tutorialspoint".getBytes(); ByteArrayInputStream bis = new ByteArrayInputStream(inBytes); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { bis.transferTo(bos); byte[] outBytes = bos.toByteArray(); System.out.println(Arrays.equals(inBytes, outBytes)); } finally { try { bis.close(); } catch(IOException e) { e.printStackTrace(); } try { bos.close(); } catch(IOException e) { e.printStackTrace(); } } } public static void main(String args[]) throws Exception { TransferToMethodTest test = new TransferToMethodTest(); test.testTransferTo(); } }
Output
true