发布网友 发布时间:2022-04-26 10:23
共2个回答
热心网友 时间:2022-06-27 08:09
java中将字符串转化成特定长度的字节数组,可以使用字符串的getBytes方法生成字节数组,实例如下:
字符串转换成字节数组热心网友 时间:2022-06-27 08:10
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MainClass {
public static void main(String args[]) throws IOException {
String source = "abc";
// 作为输入
InputStream is = new ByteArrayInputStream(source.getBytes());
int b = -1;
do {
b = is.read();
if (b != -1) {
System.out.print(b + ",");
}
} while (b != -1);
// 作为输出
OutputStream fos = new FileOutputStream(new File("d://MainClass.txt"));
OutputStream os = System.out;
os.write('\n');
os.write(source.getBytes());
fos.write(source.getBytes());
}
}
console:
97,98,99,
abc