问题描述
- java中字符串转换为字节数组请问用什么方法,字符串返回字节数组怎么做?
-
java中字符串转换为字节数组请问用什么方法,字符串返回字节数组怎么做?
解决方案
http://blog.csdn.net/shuaihj/article/details/7478773
http://blog.csdn.net/abin_gha/article/details/6317850
解决方案二:
java中String类有getBytes方法,可以将字符串转成字节数组。字节数组可以直接写入到输出流中,如写入文件输出流中,Socket输出流中等。
实例代码:
public static void main(String[] args) {
String text = "helloworld";
byte[] bytes = text.getBytes();
try {
OutputStream out = new FileOutputStream(new File("src/text.txt"));
out.write(bytes);
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
网络通信中都是用的字节数据的,所以有时候需要把字符串转成byte数组进行数据发送的。
解决方案三:
这种问题,建议你查找一个,如 Bing: Bing 查找 java中字符串转换为字节数组
解决方案四:
用现成的方法getBytes()
解决方案五:
字符串.getBytes()
时间: 2024-07-29 00:35:01