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