The transferTo() เพิ่มเมธอดใน InputStream . แล้ว คลาสใน Java 9 วิธีนี้ใช้เพื่อ คัดลอกข้อมูลจากอินพุตสตรีมไปยังเอาต์พุตสตรีม ในชวา หมายความว่าจะอ่านไบต์ทั้งหมดจากสตรีมอินพุตและเขียนไบต์ไปยังเอาต์พุตสตรีมตามลำดับที่อ่านอยู่
ไวยากรณ์
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