`
hdxiong
  • 浏览: 371519 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

InputStream与String,Byte之间互转

    博客分类:
  • Java
阅读更多
itStream outStream = nenputStream;  
import java.io.ByteArrayOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
  
/**  
 *   
 * @author Andy.Chen  
 * @mail Chenjunjun.ZJ@gmail.com  
 *  
 */  
public class InputStreamUtils {  
	  
	final static int BUFFER_SIZE = 4096;  
	  
	/**  
	 * 将InputStream转换成String  
	 * @param in InputStream  
	 * @return String  
	 * @throws Exception  
	 *   
	 */  
	public static String InputStreamTOString(InputStream in) throws Exception{  
		  
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
		byte[] data = new byte[BUFFER_SIZE];  
		int count = -1;  
		while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
			outStream.write(data, 0, count);  
		  
		data = null;  
		return new String(outStream.toByteArray(),"ISO-8859-1");  
	}  
	  
	/**  
	 * 将InputStream转换成某种字符编码的String  
	 * @param in  
	 * @param encoding  
	 * @return  
	 * @throws Exception  
	 */  
		 public static String InputStreamTOString(InputStream in,String encoding) throws Exception{  
		  
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
		byte[] data = new byte[BUFFER_SIZE];  
		int count = -1;  
		while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
			outStream.write(data, 0, count);  
		  
		data = null;  
		return new String(outStream.toByteArray(),"ISO-8859-1");  
	}  
	  
	/**  
	 * 将String转换成InputStream  
	 * @param in  
	 * @return  
	 * @throws Exception  
	 */  
	public static InputStream StringTOInputStream(String in) throws Exception{  
		  
		ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));  
		return is;  
	}  
	  
	/**  
	 * 将InputStream转换成byte数组  
	 * @param in InputStream  
	 * @return byte[]  
	 * @throws IOException  
	 */  
	public static byte[] InputStreamTOByte(InputStream in) throws IOException{  
		  
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
		byte[] data = new byte[BUFFER_SIZE];  
		int count = -1;  
		while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
			outStream.write(data, 0, count);  
		  
		data = null;  
		return outStream.toByteArray();  
	}  
	  
	/**  
	 * 将byte数组转换成InputStream  
	 * @param in  
	 * @return  
	 * @throws Exception  
	 */  
	public static InputStream byteTOInputStream(byte[] in) throws Exception{  
		  
		ByteArrayInputStream is = new ByteArrayInputStream(in);  
		return is;  
	}  
	  
	/**  
	 * 将byte数组转换成String  
	 * @param in  
	 * @return  
	 * @throws Exception  
	 */  
	public static String byteTOString(byte[] in) throws Exception{  
		  
		InputStream is = byteTOInputStream(in);  
		return InputStreamTOString(is);  
	}  

} 

转自:http://blog.csdn.net/cjjky/article/details/6892443
分享到:
评论
2 楼 hdxiong 2012-07-06  
estn_h 写道
  呵呵 谢谢了! 有2个小小的建议
1、尽量在工具类中处理异常吧
2、流的关闭操作

嗯,好建议,编程时得注意。
1 楼 estn_h 2012-07-05  
  呵呵 谢谢了! 有2个小小的建议
1、尽量在工具类中处理异常吧
2、流的关闭操作

相关推荐

    byte与各类型之间的转化

    此文档主要的内容是byte类型与各类型(String、boolean、int、inputStream等)之间的转化

    Android开发人员不得不收集的代码

    inputStream2String, string2InputStream : inputStream 与 string 按编码互转 outputStream2String, string2OutputStream: outputStream 与 string 按编码互转 bitmap2Bytes, bytes2Bitmap : bitmap 与 byteArr 互...

    java io读取文件到String

    public static String loadAFileToStringDE1(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new BufferedInputStream( ...

    java io InputStream and outputStream

    适合初学者的一些代码,public static void main(String[] args) { File file1 = new File("c:\\aaa.txt");// 定位文件 if (!file1.exists()) { System.out.println("文件不存在...."); } else { try { ...

    bytea类型上传下载图片.txt

    String strSQL = ""; int k=0,n=0; Connection dbConn = null; PreparedStatement dbStat = null; //ResultSet dbRs = null; dbConn = JDBCUtil.getConnectionELIB(); //上传...

    byte-streams:用于jvm字节表示的Rosetta Stone

    记住如何在它们之间进行转换是一项不费力的任务,而通过定义它们自己的自定义表示形式的库,或者将它们与Clojure的惰性序列和流表示形式组合在一起,使情况变得更糟。 该库是Java提供的所有字节表示形式的Rosetta...

    c# 流断点上传

    private static byte[] PostData(string serverURL, byte[] data, Hashtable parms) { System.Net.WebClient webClientObj = new System.Net.WebClient(); if (parms != null) { serverURL = serverURL + ...

    J2ME入门教程.10(j2me与Servlet相互通讯)编写和配置Servlet服务端

    String data_MIDlet = d_Inputstream.readUTF(); d_Inputstream.close(); System.out.println( "从手机上接收过来的信息:" + data_MIDlet ); String record_String = new String( data_MIDlet + " ,成功登陆" ...

    微信公众平台接口使用-连接验证(asp.net)

    byte[] b = new byte[s.Length]; s.Read(b, 0, (int)s.Length); postStr = Encoding.UTF8.GetString(b); if (!string.IsNullOrEmpty(postStr)) { //读取xml内容 XmlDocument doc = new XmlDocument(); doc....

    网页无组件上传图片与大家分享

    private byte[] b = new byte[4096]; //字节流存放数组 private boolean successful = true; private Hashtable fields = new Hashtable(); public UploadBean() { } //设置上传文件的后缀名 public void ...

    ftp网络下载

    public static byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while (...

    java用于读写数据的工具类

    有两个方法: writeData(File file,OutputStream out,String s,boolean tips); tips是打印操作结果的开关 readData(File file,InputStream in,byte[] b,boolean tips);

    android 开发中用json解析客户端与服务器端的代码

    游戏开发中客户端与服务器... byte[] data = StreamTool.readInputStream(inStream); String json = new String(data); //构建Json数组对象 JSONArray array = new JSONArray(json); //从Json数组对象读取数据

    Java文件处理工具类--FileUtil

    public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream...

    C#中压缩字符串

    //byte[] data = Convert.FromBase64String(param); MemoryStream ms = new MemoryStream(); Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms); try { stream.Write(data, 0, data....

    day019-io笔记和代码.rar

    * 2.String(byte[] bytes, int offset, int length, String charsetName) * 根据指定字符集将字节数组中从指定下标开始到指定长度结束的数据转换为字符串 * charsetName:字符集名 例如 : "GBK...

    domino访问关系性数据库例子

    byte[] bytes = null; /** * 方法说明:获得数据连接 * 输入参数: * 返回类型:Connection 连接对象 */ try { session = getSession(); agentContext = session....

    jspblog项目

    private String readLine(byte[] bytes, int[] index, ServletInputStream sis, String encoding) { try{ index[0]=sis.readLine(bytes, 0, bytes.length); if(index[0]){ return null; } }...

    Android实现TCP客户端接收数据的方法

    本文实例讲述了Android实现TCP客户端接收数据的方法。分享给大家供大家参考,具体如下: ...因此,此客户端只管通过TCP接收字符串数据,然后显示在界面上。 接收TCP数据 ... byte[] b = new byte[10000];

    使用urlconnection下载文件或图片并保存到本地

    byte[] bs = new byte[1024]; // 读取到的数据长度 int len; // 输出的文件流 OutputStream os = new FileOutputStream(filename); // 开始读取 while ((len = is.read(bs)) != -1) { os.write(bs, 0...

Global site tag (gtag.js) - Google Analytics