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
分享到:
相关推荐
此文档主要的内容是byte类型与各类型(String、boolean、int、inputStream等)之间的转化
inputStream2String, string2InputStream : inputStream 与 string 按编码互转 outputStream2String, string2OutputStream: outputStream 与 string 按编码互转 bitmap2Bytes, bytes2Bitmap : bitmap 与 byteArr 互...
public static String loadAFileToStringDE1(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new BufferedInputStream( ...
适合初学者的一些代码,public static void main(String[] args) { File file1 = new File("c:\\aaa.txt");// 定位文件 if (!file1.exists()) { System.out.println("文件不存在...."); } else { try { ...
String strSQL = ""; int k=0,n=0; Connection dbConn = null; PreparedStatement dbStat = null; //ResultSet dbRs = null; dbConn = JDBCUtil.getConnectionELIB(); //上传...
记住如何在它们之间进行转换是一项不费力的任务,而通过定义它们自己的自定义表示形式的库,或者将它们与Clojure的惰性序列和流表示形式组合在一起,使情况变得更糟。 该库是Java提供的所有字节表示形式的Rosetta...
private static byte[] PostData(string serverURL, byte[] data, Hashtable parms) { System.Net.WebClient webClientObj = new System.Net.WebClient(); if (parms != null) { serverURL = serverURL + ...
String data_MIDlet = d_Inputstream.readUTF(); d_Inputstream.close(); System.out.println( "从手机上接收过来的信息:" + data_MIDlet ); String record_String = new String( data_MIDlet + " ,成功登陆" ...
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 ...
public static byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while (...
有两个方法: writeData(File file,OutputStream out,String s,boolean tips); tips是打印操作结果的开关 readData(File file,InputStream in,byte[] b,boolean tips);
游戏开发中客户端与服务器... byte[] data = StreamTool.readInputStream(inStream); String json = new String(data); //构建Json数组对象 JSONArray array = new JSONArray(json); //从Json数组对象读取数据
public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream...
//byte[] data = Convert.FromBase64String(param); MemoryStream ms = new MemoryStream(); Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms); try { stream.Write(data, 0, data....
* 2.String(byte[] bytes, int offset, int length, String charsetName) * 根据指定字符集将字节数组中从指定下标开始到指定长度结束的数据转换为字符串 * charsetName:字符集名 例如 : "GBK...
byte[] bytes = null; /** * 方法说明:获得数据连接 * 输入参数: * 返回类型:Connection 连接对象 */ try { session = getSession(); agentContext = session....
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客户端接收数据的方法。分享给大家供大家参考,具体如下: ...因此,此客户端只管通过TCP接收字符串数据,然后显示在界面上。 接收TCP数据 ... byte[] b = new byte[10000];
byte[] bs = new byte[1024]; // 读取到的数据长度 int len; // 输出的文件流 OutputStream os = new FileOutputStream(filename); // 开始读取 while ((len = is.read(bs)) != -1) { os.write(bs, 0...