如何將char數組寫入到java socketchannel中。因爲它需要ByteBuffer,所以我無法將char數據寫入socketchannel。如何將char數組寫入到java socketchannel中
-4
A
回答
0
您是否嘗試過使用ByteBuffer的putChar方法?
ByteBuffer buf = ByteBuffer.allocate(1024);
for (char ch : myChars) {
buf.putChar(ch);
}
0
幾種方法,例如
char[] c = {'1', '2'};
String str = new String(c);
ByteBuffer bb = Charset.defaultCharset().encode(str);
或
ByteBuffer.wrap(str.getBytes());
+0
只能發表評論,你可能希望明確說明你用什麼字符集來編碼你的字符數組,而不是僅僅使用默認的字符集。你的服務器可能期待一個特定的服務器,而你不想成爲任何平臺和語言環境正在運行你的代碼的突發事件的犧牲品。 – Charlie
相關問題
- 1. 如何將char數組插入到另一個char數組中?
- 2. Java NIO SocketChannel寫入問題
- 3. 在Java中寫入SocketChannel意外重複
- 4. 將char附加到char數組java
- 5. 如何將數組寫入文件Java
- 6. Java SocketChannel寫入和讀取關聯
- 7. 如何將數據讀入char數組?
- 8. 如何從unsigned char數組寫入數據,該數組有NULL?
- 9. 如何將char []數組的內容添加到新的char []字母中? - java
- 10. 如何插入,將新元素添加到char數組中
- 11. 如何將一些char *數組放入二維數組中?
- 12. 如何將char/int添加到C中的char數組?
- 13. 如何在Java中爲'char'數組輸入數據?
- 14. 如何將struct char寫入文件
- 15. 如何在Java中將位寫入字節數組?
- 16. 如何在Java中將數組寫入outputStream
- 17. 將Char數組添加到Char指針
- 18. 寫入char *數組的字符變異
- 19. 用char數組填充char數組(char數組到int數組)
- 20. Java:將數組寫入文本文件
- 21. 將數組寫入CSV文件 - Java
- 22. 如何將char數組轉換爲java中的字符串
- 23. 在Java中將文本文件讀入char [] []數組
- 24. 如何在Java NIO中刷新SocketChannel?
- 25. java - 將數組寫入輸出文件
- 26. Java 2d char數組
- 27. 如何將數字「125」推到char數組中?
- 28. 如何將數字打印到char數組中?
- 29. 將Java字符插入到MySQL char
- 30. 如何從std :: ifstream中將文件讀入unsigned char數組?
請提供一些代碼,你的問題。 –
請參閱:http://stackoverflow.com/questions/871870/how-to-write-data-to-socket-channel。 –