 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Importance of transferTo() method of InputStream in Java 9?
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
Advertisements
 