golang写入字符串
const (
STREAM_MAGIC = 0xaced
STREAM_VERSION = 5
TC_STRING = 0x74
)
func main() {
f, _ := os.Create("test.out")
defer f.Close()
f.Write(ShortBytes(STREAM_MAGIC))
f.Write(ShortBytes(STREAM_VERSION))
f.Write([]byte{TC_STRING})
str := "85bb94b8-fd4b-4e1c-8f49-3cedd49d8f28"
strLen := len(str)
f.Write(ShortBytes(uint16(strLen)))
f.Write([]byte(str))
}
func ShortBytes(i uint16) []byte {
bytes := make([]byte, 2)
binary.BigEndian.PutUint16(bytes, i)
return bytes
}
java读取字符串
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
readTest();
}
private static void readTest() throws IOException, ClassNotFoundException {
try (var fis = new FileInputStream("test.out"); var is = new ObjectInputStream(fis)) {
var uuid = is.readObject();
System.out.print(uuid);
}
}
}
有疑问加站长微信联系(非本文作者)