Several methods of mutual transfer between string and InputStream

Source: Internet
Author: User

  1. /**
  2. * Use BufferedReader to convert InputStream to string < function description >
  3. *
  4. * @param in
  5. * @return String
  6. */
  7. public static string Inputstr2str_reader (InputStream in, string encode)
  8. {
  9. String str = "";
  10. Try
  11. {
  12. if (encode = = Null | | encode.equals (""))
  13. {
  14. //default in utf-8 form
  15. encode = "Utf-8";
  16. }
  17. BufferedReader reader = new BufferedReader (new InputStreamReader (in, encode));
  18. StringBuffer sb = new StringBuffer ();
  19. While ((str = reader.readline ()) = null)
  20. {
  21. Sb.append (str). Append ("\ n");
  22. }
  23. return sb.tostring ();
  24. }
  25. catch (unsupportedencodingexception E1)
  26. {
  27. E1.printstacktrace ();
  28. }
  29. catch (IOException e)
  30. {
  31. E.printstacktrace ();
  32. }
  33. return str;
  34. }
  35. /** 
  36. * Using byte array conversion inputstream------->string < function description >
  37. *
  38. * @param in
  39. * @return
  40. * @see [Class, Class # method, Class # member]
  41. */
  42. public static string Inputstr2str_bytearr (InputStream in, string encode)
  43. {
  44. StringBuffer sb = new StringBuffer ();
  45. byte[] B = new byte[1024];
  46. int len = 0;
  47. Try
  48. {
  49. if (encode = = Null | | encode.equals (""))
  50. {
  51. //default in utf-8 form
  52. encode = "Utf-8";
  53. }
  54. While (len = In.read (b))! =-1)
  55. {
  56. Sb.append (new String (b, 0, Len, encode));
  57. }
  58. return sb.tostring ();
  59. }
  60. catch (IOException e)
  61. {
  62. E.printstacktrace ();
  63. }
  64. return "";
  65. }
  66. /** 
  67. * Use Bytearrayoutputstream:inputstream------------>string < function description >
  68. *
  69. * @param in
  70. * @return
  71. * @see [Class, Class # method, Class # member]
  72. */
  73. public static String Inputstr2str_bytearrayoutputstream (InputStream in,string encode)
  74. {
  75. Bytearrayoutputstream out = new Bytearrayoutputstream ();
  76. byte[] B = new byte[1024];
  77. int len = 0;
  78. Try
  79. {
  80. if (encode = = Null | | encode.equals (""))
  81. {
  82. //default in utf-8 form
  83. encode = "Utf-8";
  84. }
  85. While (len = In.read (b)) > 0)
  86. {
  87. Out.write (b, 0, Len);
  88. }
  89. return out.tostring (encode);
  90. }
  91. catch (IOException e)
  92. {
  93. E.printstacktrace ();
  94. }
  95. return "";
  96. }
  97. /** 
  98. * Use bytearrayinputstream:string------------------>inputstream < function description >
  99. *
  100. * @param inStr
  101. * @return
  102. * @see [Class, Class # method, Class # member]
  103. */
  104. public static InputStream str2inputstr (String inStr)
  105. {
  106. Try
  107. {
  108. //return new Bytearrayinputstream (Instr.getbytes ());
  109. //return new Bytearrayinputstream (Instr.getbytes ("UTF-8"));
  110. return new StringBufferInputStream (INSTR);
  111. }
  112. catch (Exception e)
  113. {
  114. E.printstacktrace ();
  115. }
  116. return null;
  117. }
=====================================android Read TXT file garbled solution: Read Inputsteam when the "GB2312" way to read, pay attention to the pure use of Retstr = Encodingutils.getstring (Retstr.getbytes (), "GB2312"), it is not possible to instantiate the retstr when it should be "GB2312" way. The following is reproduced in the content:
From SDcard saved TXT file read Chinese to Android system will appear garbled problem, how to solve this garbled problem, online there are many solutions, such as the use of string Temp1 =encodingutils.getstring ( Strline.getbytes (), "GB2312"); But not all the situation is applicable to solve garbled problems first to understand why garbled. The reason is because the TXT file is saved on the win system by default to ANSI format, and Android currently only supports UTF-8 encoding, so the TXT file in the Chinese read into the Android system will produce garbled. Some people say that TXT is saved directly as UTF-8 encoding format to solve the garbled problem, but this method is not a permanent solution, can not require users to manually change the format, customer first. Therefore, it is necessary to find ways to handle the process in the program.
Here are some coding format tests:

Test text: 122.11196, 29.90573, Beilun solid waste factory test Code snippet:

Reader=new BufferedReader (new FileReader (filename));

Strline=reader.readline () ;

String Temp1 = encodingutils.getstring (Strline.getbytes (), "GB2312");

String Temp2 = encodingutils.getstring (Strline.getbytes ("Utf-8"), "Utf-8");

String Temp3 = encodingutils.getstring (Strline.getbytes (), "utf-8");

Save a file in Unicode format

Save a file in Utf-8 format

This way can get non-garbled Chinese display, but for the UTF-8 format obtained by the latitude and longitude of the digital use of Double lon = double.parsedouble (LAT); Error NumberFormatException,原因可能是 the Parsedouble (LAT) method cannot handle punctuation decimals stored in the utf-8 format. Save a file in ANSI format

Change the code to:

reader = new BufferedReader (new InputStreamReader (new FileInputStream (filename), "GB2312"));

Strline=reader.readline () ;

String Temp1 = encodingutils.getstring (Strline.getbytes (), "GB2312");

String Temp2 = encodingutils.getstring (Strline.getbytes ("Utf-8"), "Utf-8");

String Temp3 = encodingutils.getstring (Strline.getbytes (), "utf-8");

That solves the problem of Chinese garbled, but also solves the problem of double.parsedouble (LAT) error.

Several methods of mutual transfer between string and InputStream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.