- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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