- /**
- * Use BufferedReader to convert InputStream to string < function description >
- *
- * @param in
- * @return String
- */
- public static string Inputstr2str_reader (InputStream in, string encode)
- {
- String str = "";
- Try
- {
- if (encode = = Null | | encode.equals (""))
- {
- //default in utf-8 form
- encode = "Utf-8";
- }
- BufferedReader reader = new BufferedReader (new InputStreamReader (in, encode));
- StringBuffer sb = new StringBuffer ();
- While ((str = reader.readline ()) = null)
- {
- Sb.append (str). Append ("\ n");
- }
- return sb.tostring ();
- }
- catch (unsupportedencodingexception E1)
- {
- E1.printstacktrace ();
- }
- catch (IOException e)
- {
- E.printstacktrace ();
- }
- return str;
- }
- /**
- * Using byte array conversion inputstream------->string < function description >
- *
- * @param in
- * @return
- * @see [Class, Class # method, Class # member]
- */
- public static string Inputstr2str_bytearr (InputStream in, string encode)
- {
- StringBuffer sb = new StringBuffer ();
- byte[] B = new byte[1024];
- int len = 0;
- Try
- {
- if (encode = = Null | | encode.equals (""))
- {
- //default in utf-8 form
- encode = "Utf-8";
- }
- While (len = In.read (b))! =-1)
- {
- Sb.append (new String (b, 0, Len, encode));
- }
- return sb.tostring ();
- }
- catch (IOException e)
- {
- E.printstacktrace ();
- }
- return "";
- }
- /**
- * Use Bytearrayoutputstream:inputstream------------>string < function description >
- *
- * @param in
- * @return
- * @see [Class, Class # method, Class # member]
- */
- public static String Inputstr2str_bytearrayoutputstream (InputStream in,string encode)
- {
- Bytearrayoutputstream out = new Bytearrayoutputstream ();
- byte[] B = new byte[1024];
- int len = 0;
- Try
- {
- if (encode = = Null | | encode.equals (""))
- {
- //default in utf-8 form
- encode = "Utf-8";
- }
- While (len = In.read (b)) > 0)
- {
- Out.write (b, 0, Len);
- }
- return out.tostring (encode);
- }
- catch (IOException e)
- {
- E.printstacktrace ();
- }
- return "";
- }
- /**
- * Use bytearrayinputstream:string------------------>inputstream < function description >
- *
- * @param inStr
- * @return
- * @see [Class, Class # method, Class # member]
- */
- public static InputStream str2inputstr (String inStr)
- {
- Try
- {
- //return new Bytearrayinputstream (Instr.getbytes ());
- //return new Bytearrayinputstream (Instr.getbytes ("UTF-8"));
- return new StringBufferInputStream (INSTR);
- }
- catch (Exception e)
- {
- E.printstacktrace ();
- }
- return null;
- }
=====================================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