zoukankan      html  css  js  c++  java
  • String与InputStream互转的几种方法

    1. /** 
    2.     * 利用BufferedReader实现Inputstream转换成String <功能详细描述> 
    3.     *  
    4.     * @param in 
    5.     * @return String 
    6.     */  
    7.      
    8.    public static String Inputstr2Str_Reader(InputStream in, String encode)  
    9.    {  
    10.          
    11.        String str = "";  
    12.        try  
    13.        {  
    14.            if (encode == null || encode.equals(""))  
    15.            {  
    16.                // 默认以utf-8形式  
    17.                encode = "utf-8";  
    18.            }  
    19.            BufferedReader reader = new BufferedReader(new InputStreamReader(in, encode));  
    20.            StringBuffer sb = new StringBuffer();  
    21.              
    22.            while ((str = reader.readLine()) != null)  
    23.            {  
    24.                sb.append(str).append(" ");  
    25.            }  
    26.            return sb.toString();  
    27.        }  
    28.        catch (UnsupportedEncodingException e1)  
    29.        {  
    30.            e1.printStackTrace();  
    31.        }  
    32.        catch (IOException e)  
    33.        {  
    34.            e.printStackTrace();  
    35.        }  
    36.          
    37.        return str;  
    38.    }  
    39.      
    40.    /** 
    41.     * 利用byte数组转换InputStream------->String <功能详细描述> 
    42.     *  
    43.     * @param in 
    44.     * @return 
    45.     * @see [类、类#方法、类#成员] 
    46.     */  
    47.      
    48.    public static String Inputstr2Str_byteArr(InputStream in, String encode)  
    49.    {  
    50.        StringBuffer sb = new StringBuffer();  
    51.        byte[] b = new byte[1024];  
    52.        int len = 0;  
    53.        try  
    54.        {  
    55.            if (encode == null || encode.equals(""))  
    56.            {  
    57.                // 默认以utf-8形式  
    58.                encode = "utf-8";  
    59.            }  
    60.            while ((len = in.read(b)) != -1)  
    61.            {  
    62.                sb.append(new String(b, 0, len, encode));  
    63.            }  
    64.            return sb.toString();  
    65.        }  
    66.        catch (IOException e)  
    67.        {  
    68.            e.printStackTrace();  
    69.        }  
    70.        return "";  
    71.          
    72.    }  
    73.      
    74.    /** 
    75.     * 利用ByteArrayOutputStream:Inputstream------------>String <功能详细描述> 
    76.     *  
    77.     * @param in 
    78.     * @return 
    79.     * @see [类、类#方法、类#成员] 
    80.     */  
    81.    public static String Inputstr2Str_ByteArrayOutputStream(InputStream in,String encode)  
    82.    {  
    83.        ByteArrayOutputStream out = new ByteArrayOutputStream();  
    84.        byte[] b = new byte[1024];  
    85.        int len = 0;  
    86.        try  
    87.        {  
    88.            if (encode == null || encode.equals(""))  
    89.            {  
    90.                // 默认以utf-8形式  
    91.                encode = "utf-8";  
    92.            }  
    93.            while ((len = in.read(b)) > 0)  
    94.            {  
    95.                out.write(b, 0, len);  
    96.            }  
    97.            return out.toString(encode);  
    98.        }  
    99.        catch (IOException e)  
    100.        {  
    101.            e.printStackTrace();  
    102.        }  
    103.        return "";  
    104.    }  
    105.      
    106.    /** 
    107.     * 利用ByteArrayInputStream:String------------------>InputStream <功能详细描述> 
    108.     *  
    109.     * @param inStr 
    110.     * @return 
    111.     * @see [类、类#方法、类#成员] 
    112.     */  
    113.    public static InputStream Str2Inputstr(String inStr)  
    114.    {  
    115.        try  
    116.        {  
    117.            // return new ByteArrayInputStream(inStr.getBytes());  
    118.            // return new ByteArrayInputStream(inStr.getBytes("UTF-8"));  
    119.            return new StringBufferInputStream(inStr);  
    120.        }  
    121.        catch (Exception e)  
    122.        {  
    123.            e.printStackTrace();  
    124.        }  
    125.        return null;  
    126.    }  

    =====================================

    Android读取txt文件乱码解决方案:
    读取inputsteam的时候以“GB2312”方式读取,注意单纯的利用retStr =EncodingUtils.getString(retStr.getBytes(), "GB2312");是不行的,实例化retStr的时候就应该以“GB2312”方式。
    以下是转载的内容:
    从SDCard保存的txt文件读取中文到android系统中会出现乱码问题,如何解决这个乱码问题,网上有不少解答方法,譬如说利用String temp1 =EncodingUtils.getString(strLine.getBytes(),"GB2312"); 但并非对所有的情况都适用,解决乱码问题首先要明白为什么会乱码。究其原因,是因为txt文件在win系统上保存时默认为ANSI格式,而android目前只支持UTF-8编码,因此将txt文件的中文读入android系统中会产生乱码。也有人说直接将txt另存为UTF-8编码格式来解决乱码问题,但这种方法指标不治本,不能要求用户手动去更改格式,客户第一嘛。因此还是需要想办法在程序中进行处理。
    以下做了一些编码格式的测试:

    测试文本: 122.11196,29.90573,北仑固废厂 测试代码段:

    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");

    将文件存成 Unicode 格式

    将文件存成utf-8 格式

    这种方式能得到非乱码的中文显示,但对于 utf-8 格式下取得的经纬度数字利用double lon = Double.parseDouble(lat); 报错 NumberFormatException,原因可能是 parseDouble(lat)方法不能处理存成utf-8格式的带标点小数。 将文件 存成 ANSI 格式

    将代码改为:

    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");

    即解决了中文乱码问题,又解决了Double.parseDouble(lat)报错问题。

     

  • 相关阅读:
    ASP.NET MVC 学习之路由(URL Routing)
    Linux 部署ASP.NET SQLite 应用 的坎坷之旅 附demo及源码
    linux解压zip文件
    /bin/sh^M:解释器错误:没有那个文件或目录
    mysql查看连接情况
    linux编译qt
    没有可用的软件包 xxx,但是它被其它的软件包引用了
    什么是人月
    qt linux 打包
    linux里安装使用svn
  • 原文地址:https://www.cnblogs.com/likeju/p/5103291.html
Copyright © 2011-2022 走看看