入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

string 与 byte[] 的相互转化

创建时间:2005-05-25 投稿人: 浏览次数:9896

 1、String 到 byte[]
String str = new String("hello world");
byte[] by = str.getBytes();

2、byte[] 到String
String str = new String("hello world");
byte[] by = str.getBytes();
String s = new String(by);还可以设置offset,在读取文件的二进制流时候特别有用,需要把二进制流直接变为String,可以这样:

StringBuffer strBf = new StringBuffer("");
InputStream stream = file.getInputStream(); //把文件读入
byte[] buffer = new byte[8192];
  while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1)
  {
           strBf.append(new String(buffer,0,bytesRead));//生成字符串
  }

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像