transferTo() मेथड को जावा 9 में InputStream क्लास में जोड़ा गया है। इस मेथड का उपयोग जावा में इनपुट स्ट्रीम से आउटपुट स्ट्रीम में डेटा कॉपी करने के लिए किया जाता है। इसका मतलब है कि यह इनपुट स्ट्रीम से सभी बाइट्स पढ़ता है और उन्हें उसी क्रम में आउटपुट स्ट्रीम में लिखता है जिसमें वे पढ़े जा रहे हैं।
सिंटैक्स
public long transferTo(OutputStream out) throws IOException
उदाहरण
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();
}
}
आउटपुट
true