String与InputStream互转的几种方法
[java] view plain copy
- /**
- * 利用BufferedReader实现Inputstream转换成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数组转换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;
- }
- /**
- * 利用BufferedReader实现Inputstream转换成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数组转换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;
- }
=====================================
从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)报错问题
转自:
String与InputStream互转的几种方法的更多相关文章
- String 和 InputStream 互转方式
/** * 利用BufferedReader实现Inputstream转换成String <功能详细描述> * * @param in * @return String */ public ...
- String和inputstream互转【转文】
URLConnection urlConn = url.openConnection(); // 打开网站链接s BufferedReader reader = new BufferedReader( ...
- JavaScript中unicode编码与String互转(三种方法)
1.引言 JS本身就支持unicode转string功能,一共有三种方式和String单个字符转unicode编码. 2.方法 //unicode转String 1. eval("'&quo ...
- 在 Linux 下将 PNG 和 JPG 批量互转的四种方法
计算机术语中,批处理指的是用一个非交互式的程序来执行一序列的任务[1]的方法.这篇教程里,我们会使用 Linux 命令行工具,并提供 4 种简单的处理方式来把一些 .PNG 格式的图像批量转换成 .J ...
- java实现map和object互转的三种方法
/** * 使用org.apache.commons.beanutils进行转换 */ class A { public static Object mapToObje ...
- JavaScript 字符串与json对象互转的几种方法
第一种:浏览器支持的转换方式(Firefox,chrome,opera,safari,ie)等浏览器: JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON. ...
- SQL SERVER 将表字段值0和1互转的几种方法
需求: 如果表字段的值为 0 则将其修改为1 ,如果表字段的值为 1 则将其修改为 0. 方法一 end 方法二 ) 方法三 )
- int 和String之间的互转
int -> String int i=12345;String s="";第一种方法:s=i+"";第二种方法:s=String.valueOf(i); ...
- java中定时器的四种方法
package com.lid; import java.util.Calendar; import java.util.Date; import java.util.Timer; import ja ...
随机推荐
- 运算符++,--的使用及 while循环测试的用处
前++与后++的区别: b=3+(++a);//a=a+1;b=3+a; b=3+(a++);//b=3+a;a=a+1; while (true)( 循环语句,可以实现程序的多次测试) { Cons ...
- WSADATA
中文名 WSADATA 操作系统 Windows CE 版本: 大于等于1.0 定义位置: Winsock.h 目录 1 说明 2 结构原型 3 参数说明 4 备注 说明编辑 WSADATA结构被用来 ...
- 史上最全的Linux常用命令
系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...
- JavaWeb学习笔记——Tomcat相关
Tomcat目录分析 1.bin 存放启动和关闭Tomcat的脚本文件 2.conf 存放Tomcat服务器的各种配置文件 3.lib 存放Tomcat服务器的支持jar包 4.logs 存放T ...
- Mac OS下配置Eclipse C++的方法
http://nonlz.blog.163.com/blog/static/128872032201262622921622/
- GIT安装完需要做以下配置
安装完GIT后需要做以下配置: 一.找到git的安装目录,查找etc目录下的gitconfig配置文件,编辑此文件在最后一行添加如下内容: [gui] encoding = utf-8 [sv ...
- VS2013-解决error C4996: 'fopen'问题
VS2013中如何解决error C4996: 'fopen'问题 初次使用vs系列编辑器编写控制台应用程序时常出现如下错误: error C4996: 'fopen': This function ...
- Tween + 缓动函数
Unity-Tween http://www.cnblogs.com/MrZivChu/p/UnityTween.html iTween: iTween大解构(一)之抛物线移动 http://blog ...
- Ios 消息推送
手把手教你做iOS推送 http://www.cocoachina.com/industry/20130321/5862.html http://www.cnblogs.com/cdts_change ...
- C++ compile issue
You can do so via right-click -> Properties on a file or a selection of files in Solution Explore ...