Java字节输出流问题

    byte[] bytes = "我是中国人".getBytes();
    os1.write(bytes);
    os1.flush();
    os1.write("\n".getBytes());

    byte[] bytes1 ={'2','3','a'};
    os1.write(bytes1);
    os1.flush();
    os1.write("\n".getBytes());

请问为什么第一个字节数组需要getBytes()方法编码后才能输出,第二个字节数组则不需要呢,是因为第一个是字符串,第二个是字符吗?

因为第一个 是字符串,转字节数组需要调用String的内置方法

img

第二个其实是字节类型和char类型,可以直接转换

第一句拆开来写,其实应该是 String string = "我是中国人"; byte[] bytes =string.getBytes();

所以,是因为"我是中国人"是字符串,需要转为byte数组,而第二个直接就是byte数组了。