/** * Using BufferedReader to convert Inputstream into String * * @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; } /** * Converting InputStream with byte arrays - ---> String < Functional Detailed 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 ""; } /** * Using Byte Array Output Stream: Inputstream - ----------> String < Functional Detailed 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 ""; } /** * Using Byte Array InputStream: String - ----------------------> InputStream < Functional Detailed 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 scrambling solution:
When reading inputsteam, read it in the way of "GB2312". Pay attention to using retStr =EncodingUtils.getString(retStr.getBytes(), "GB2312"); it is not possible. When you instantiate retStr, you should use the way of "GB2312".
The following is reprinted:
There are many ways to solve this problem, such as using String temp1 = Encoding Utils. getString (strLine. getBytes (), "GB2312"); but it is not applicable to all cases. To solve this problem, first of all, we need to clarify. Why does white scramble? The reason is that when txt files are saved on win system, they are in ANSI format by default, while android only supports UTF-8 encoding at present. Therefore, reading the Chinese of txt files into android system will cause chaos. Others say that TXT is directly saved as UTF-8 encoding format to solve the problem of scrambling, but this method does not cure the root cause, can not require users to manually change the format, customers first. Therefore, we still need to find ways to deal with it in the program.
Following are some tests of the encoding format:
Test Text: 122.11196, 29.90573, Beilun Solid Waste Plant Test Code Section:
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 files in Unicode format
Store files in utf-8 format
This way can get non-random Chinese display, but for longitude and latitude digits in utf-8 format, double lon = Double.parseDouble(lat) is used; NumberFormatException is reported, probably because parseDouble(lat) method can not process punctuated decimal numbers stored in utf-8 format. Save files 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");
It not only solves the problem of Chinese random code, but also solves the problem of Double.parseDouble(lat) error reporting.