/**
* 利用BufferedReader實現(xiàn)Inputstream轉(zhuǎn)換成String <功能詳細描述>
*
* @param in
* @return String
*/
public static String Inputstr2Str_Reader(InputStream in, String encode)
{
String str = "";
try
{
if (encode == null || encode.equals(""))
{
// 默認以utf-8形式
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;
}
/**
* 利用byte數(shù)組轉(zhuǎn)換InputStream------->String <功能詳細描述>
*
* @param in
* @return
* @see [類驶悟、類#方法、類#成員]
*/
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(""))
{
// 默認以utf-8形式
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 "";
}
/**
* 利用ByteArrayOutputStream:Inputstream------------>String <功能詳細描述>
*
* @param in
* @return
* @see [類、類#方法磅叛、類#成員]
*/
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(""))
{
// 默認以utf-8形式
encode = "utf-8";
}
while ((len = in.read(b)) > 0)
{
out.write(b, 0, len);
}
return out.toString(encode);
}
catch (IOException e)
{
e.printStackTrace();
}
return "";
}
/**
* 利用ByteArrayInputStream:String------------------>InputStream <功能詳細描述>
*
* @param inStr
* @return
* @see [類睦疫、類#方法道盏、類#成員]
*/
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讀取txt文件亂碼解決方案:
讀取inputsteam的時候以“GB2312”方式讀取莹菱,注意單純的利用retStr =EncodingUtils.getString(retStr.getBytes(), "GB2312");是不行的移国,實例化retStr的時候就應(yīng)該以“GB2312”方式。
以下是轉(zhuǎn)載的內(nèi)容:
從SDCard保存的txt文件讀取中文到android系統(tǒng)中會出現(xiàn)亂碼問題芒珠,如何解決這個亂碼問題桥狡,網(wǎng)上有不少解答方法搅裙,譬如說利用String temp1 =EncodingUtils.getString(strLine.getBytes(),"GB2312"); 但并非對所有的情況都適用皱卓,解決亂碼問題首先要明白為什么會亂碼。究其原因部逮,是因為txt文件在win系統(tǒng)上保存時默認為ANSI格式娜汁,而android目前只支持UTF-8編碼,因此將txt文件的中文讀入android系統(tǒng)中會產(chǎn)生亂碼兄朋。也有人說直接將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 格式下取得的經(jīng)緯度數(shù)字利用double lon = Double.parseDouble(lat); 報錯 NumberFormatException蹭越,原因可能是 parseDouble(lat)方法不能處理存成utf-8格式的帶標點小數(shù)障本。 將文件 存成 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)報錯問題响鹃。
轉(zhuǎn)載