Python 到 Java 代码:String.encode() in java

Python to Java code: String.encode() in java

我正在尝试使用 Raspberry Pi 模拟键盘,并在网上找到了一些实现该功能的代码。在 python 基础上它可以运行,但现在我想将它转换为 Java(我的主要语言)以便更多地使用它。

这是 python 代码的重要部分:

def write_report(report):
    print(report.encode())
    # with open('/dev/hidg0', 'rb+') as fd:
        # fd.write(report.encode())

# Press a
write_report(NULL_CHAR*2+chr(4)+NULL_CHAR*5)

所以输出如下:
b'\x00\x00\x04\x00\x00\x00\x00\x00'

如何将相同的输出写入 java 中的文件?

我找到了一种将相同字节写入文件的方法。这是代码:

    byte[] bytes = new byte[] {0,0,4,0,0,0,0,0};
    FileOutputStream fWriter;
    try {
        fWriter = new FileOutputStream(new File("D:\testJava.txt"));
        fWriter.write(bytes);
    } catch (IOException e) {
        e.printStackTrace();
    }

谢谢!