我一直在閱讀Java Tutorials on I/O以試圖瞭解流和它們的正確用法。假設我有兩個連接的設備,並且兩個設備上都有一個InputStream
和OutputStream
。我如何在兩者之間傳輸數據?使用流在Java中傳輸數據
例如,如果我想讓一臺設備向另一臺設備發送一串文字,然後將這些文字打印到屏幕上。這將如何工作?
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?
*/
}
也許我正在做所有這些錯誤。提前感謝您的理解幫助。
您的2個設備是否在同一個JVM會話中運行? – assylias 2012-07-09 16:43:59
單詞如何分隔?按空格?做多個空間問題? – 2012-07-09 16:44:43
@assylias我想象兩個完全獨立的設備(例如Android設備和桌面)。 – Whymarrh 2012-07-09 16:47:00