String與InputStream互轉的幾種方法


[java]  view plain  copy
  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("\n");  
  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)報錯問題    

    轉自:

    http://blog.csdn.net/iplayvs2008/article/details/11484627


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM