macoo

记录收获的点点滴滴

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

需要用的的两个转码方法:

import java.io.*;

public class Test{
/**
* 将字节数组转换为字符串
*
@param bytes 需要转换的字节数组
*
@return 转换后的字符串
*/
public static String byte2String(byte[] bytes){
try{
ByteArrayInputStream bais
= new ByteArrayInputStream(bytes);
DataInputStream dis
= new DataInputStream(bais);
String s
= dis.readUTF();
//关闭流
dis.close();
bais.close();
return s;
}
catch(Exception e){
return null;
}

 

/**
* 将字符串转换为字节数组
*
@param s 需要转换的字符串
*
@return 转换后生成的字节数组
*/
public static byte[] string2Byte(String s){
try{
ByteArrayOutputStream baos
= new ByteArrayOutputStream();
DataOutputStream bos
= new DataOutputStream(baos);
bos.writeUTF(s);
byte[] bytes = baos.toByteArray();
//关闭流
bos.close();
baos.close();
return bytes;
}
catch(Exception e){
return null;
}
}
}

 

posted on 2010-03-09 10:24  macoo  阅读(220)  评论(0)    收藏  举报