从byte []读/写

时间:2015-12-12 16:24:00

标签: java arrays byte

java中有没有办法从byte []读取/写入整数,布尔和字符串?

Like:
write(50550);
write(true);
write("bla bla");

And then:
int i = readInt();
int j = readBoolean();
String res = readUTF();

Walter和fge回答了两种不同的方式来实现这个目标。唯一的问题仍然存在:“哪种方式更快?”

3 个答案:

答案 0 :(得分:4)

您要找的是DataInputStreamDataOutputStream

两者都包裹resp.an InputStreamOutputStream,可以读/写各种基本类型,“转换的UTF”数据等。

至于写入一个字节数组本身,你想使用ByteArrayOutputStream,并将DataOutputStream包裹在它上面:

final ByteArrayOutputStream out = new ByteArrayOutputStream();
final DataOutputStream dataOut = new DataOutputStream(out);

// use dataOut; then:

final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
final DataInputStream dataIn = new DataInputStream(in);

// read back

无论如何,在你的一条评论中,还不清楚你想做什么;如果这只是二进制数据,则转而使用ByteBuffer;您可以在使用它之前随意更改其字节序(默认情况下是大端),在其中包装字节数组等等......不同之处在于Java没有无符号原始整数类型(嗯,保存为{{1但这个是一个奇特的野兽。)

总而言之,这看起来像一个XY问题,所以我建议你编辑你的问题并完全解释你想要做什么。

答案 1 :(得分:2)

    ByteBuffer buffer = ByteBuffer.allocate(0x1000);
    buffer.order(ByteOrder.BIG_ENDIAN);

    buffer.putInt(100);
    //store booleans as 1 or 0 in a byte
    buffer.put((byte) (true ? 1 : 0));
    buffer.put((byte) (false ? 1 : 0));

    //store string as [int length, byte array]
    String str = "Hello World!";
    byte[] strBytes = str.getBytes(StandardCharsets.UTF_8); //use any charset you want here
    buffer.putInt(strBytes.length);
    buffer.put(strBytes);


    buffer.flip();
    byte[] data = new byte[buffer.remaining()];
    buffer.get(data);
    System.out.println("Total Bytes: " + data.length);


    ByteBuffer bb = ByteBuffer.wrap(data);

    System.out.println(bb.getInt());
    System.out.println(bb.get() != 0);
    System.out.println(bb.get() != 0);
    byte[] inStr = new byte[bb.getInt()];
    bb.get(inStr);
    String myStr = new String(inStr, StandardCharsets.UTF_8);
    System.out.println(myStr);

答案 2 :(得分:0)

您似乎正在从DataInputStream和DataOutputStream中搜索。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
// Like:
dos.writeInt(50550);
dos.writeBoolean(true);
dos.writeUTF("bla bla");
byte[] bytes = baos.toByteArray();

DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
// And then:
int i = dis.readInt();
boolean flag = dis.readBoolean();
String res = dis.readUTF();

System.out.println("i: " + i + ", flag: " + flag + ", res: '" + res + "'");

打印

i: 50550, flag: true, res: 'bla bla'
  

stdext :: writeULE16,stdext :: writeULE32,stdext :: writeULE64和stdext :: readULE16,stdext :: readULE32,stdext :: readULE64

这意味着您需要一种文本格式,在这种情况下我建议使用PrintWriter

ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, "UTF-16LE"));
// Like:
pw.println(50550);
pw.println(true);
pw.println("bla bla");
pw.close();
byte[] bytes = baos.toByteArray();

Scanner in = new Scanner(new InputStreamReader(new ByteArrayInputStream(bytes), "UTF-16LE"));
// And then:
int i = in.nextInt();
boolean flag = in.nextBoolean(); in.nextLine(); // discard the rest of the line
String res = in.nextLine();

System.out.println("i: " + i + ", flag: " + flag + ", res: '" + res + "'");

打印相同的内容,但底层字节将采用UTF-16LE编码。这种方法还支持UTF-16BE,UTF-32LE,UTF-32BE(和UTF-8)