我一直在阅读有关 I/O 的 Java 教程,试图了解流及其正确用法。假设我有两个连接的设备,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<String> words = new ArrayList<String>();
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?
*/
}
也许我做错了所有这些。在此先感谢您对理解的任何帮助。