如何將InputStream轉換爲BlackBerry上的字符串?BlackBerry InputStream轉換爲字符串
3
A
回答
4
我會將inputStream中的數據存儲在StringBuffer中。代碼如下所示:
byte[] buffer = new byte[1024];
StringBuffer sb = new StringBuffer();
int readIn = 0;
while((readIn = inputStream.read(buffer)) > 0)
{
String temp = new String(buffer, 0, readIn);
sb.append(temp);
}
String result = sb.toString();
2
Jonathan的方法假定您的字節表示默認BlackBerry編碼(ISO-8859-1)中的字符串。然而大多數現代應用都是多語言的,所以支持的最好的編碼是UTF-8。要尊重一組編碼你可以使用水木清華這樣的:
/**
* Constructs a String using the data read from the passed InputStream.
* Data is read using a 1024-chars buffer. Each char is created using the passed
* encoding from one or more bytes.
*
* <p>If passed encoding is null, then the default BlackBerry encoding (ISO-8859-1) is used.</p>
*
* BlackBerry platform supports the following character encodings:
* <ul>
* <li>"ISO-8859-1"</li>
* <li>"UTF-8"</li>
* <li>"UTF-16BE"</li>
* <li>"UTF-16LE"</li>
* <li>"US-ASCII"</li>
* </ul>
*
* @param in - InputStream to read data from.
* @param encoding - String representing the desired character encoding, can be null.
* @return String created using the char data read from the passed InputStream.
* @throws IOException if an I/O error occurs.
* @throws UnsupportedEncodingException if encoding is not supported.
*/
public static String getStringFromStream(InputStream in, String encoding) throws IOException {
InputStreamReader reader;
if (encoding == null) {
reader = new InputStreamReader(in);
} else {
reader = new InputStreamReader(in, encoding);
}
StringBuffer sb = new StringBuffer();
final char[] buf = new char[1024];
int len;
while ((len = reader.read(buf)) > 0) {
sb.append(buf, 0, len);
}
return sb.toString();
}
13
這個怎麼樣了最少的代碼:
String str = new String(IOUtilities.streamToBytes(is), "UTF-8");
相關問題
- 1. java將inputStream轉換爲base64字符串
- 2. BlackBerry將字符串轉換爲HTML
- 3. 將SAX ContentHandler字符(..)轉換爲InputStream
- 4. 無法將字符串轉換爲InputStream來解析XML
- 5. 將InputStream的Latin-1內容轉換爲UTF-8字符串
- 6. 如何將InputStream轉換爲Java中的字符串?
- 7. 字符串轉換爲字節轉換
- 8. 將字符串轉換爲字符串
- 9. 轉換數字字符串轉換爲字符串
- 10. 的Node.js:XML轉換爲字符串轉換
- 11. NSDate()轉換爲字符串
- 12. 轉換字符串爲BigDecimal
- 13. 字符串轉換爲
- 14. 字符串轉換爲DateTime
- 15. 數轉換爲字符串
- 16. XMLConfiguration轉換爲字符串
- 17. Java:inetaddress轉換爲字符串
- 18. 字符串轉換爲DateTime
- 19. 將字符串轉換爲
- 20. 轉換字符串[] []爲String []
- 21. 字符串轉換爲HttpPostedFileBase
- 22. 字符串轉換爲time_t
- 23. 轉換_int64爲字符串
- 24. 字符串轉換爲NSDecimalNumber
- 25. 將字符串轉換爲「_」
- 26. 字符串轉換爲u_char *
- 27. com.sun.org.apache.xerces.internal.dom.DeferredElementImpl轉換爲字符串
- 28. 字符串轉換爲
- 29. 將字符串轉換爲?
- 30. 字符串轉換爲INT64
+1,真簡約! – 2010-12-03 22:06:32