我一直在阅读Java Tutorials on I/O以试图理解流和它们的正确用法。假设我有两台已连接的设备,并且两台设备上都有InputStream和OutputStream。我如何在两者之间传输数据?
例如,如果我想让一台设备向另一台设备发送一串文字,然后将其打印到屏幕上。这将如何工作?
public class Device1 {
// assuming connectedDevice is something
String[] words = new String()[]{"foo", "bar", "baz"};
OutputStream os = connectedDevice.getOutputStream();
InputStream is = connectedDevice.getInputStream();
/*
How to write to output stream?
*/
}
public class Device2 {
// assuming connectedDevice is something
ArrayList words = new ArrayList();
OutputStream os = connectedDevice.getOutputStream();
InputStream is = connectedDevice.getInputStream();
/*
How can I somehow get the words using `read()` and `put()`
them into the ArrayList for use?
*/
}也许我正在做这一切错误。提前感谢您的理解帮助。