gpt4 book ai didi

Java - 发送到 OutputStream 的两个字符串之间的差异

转载 作者:行者123 更新时间:2023-12-02 03:37:56 26 4
gpt4 key购买 nike

  • 我将 2 个字符串值从 Client.java 发送到 OutputStream,如下所示:

    outputStream.write(username.getText().getBytes());
    outputStream.write(password.getText().getBytes());
  • 在 Server.java 中,当我读取 inputStream 时,我试图将每个值分开:

    inputStream = s.getInputStream();
    byte[]用户名 = 新字节[20];
    inputStream.read(用户名);
    String user = new String(用户名);
    System.out.println("用户名 = "+user);

我逻辑上得到:usernamepassword 作为控制台输出。我想做的是:

     String usr = new String(user);
String pass = new String(password);

有没有比在输出流字符串中添加一些分隔符更好的方法?

最佳答案

您需要分隔两个字符串值,以便读者知道一个字符串在哪里结束以及下一个字符串在哪里开始。该分隔符的实际组成由您根据您的特定需求决定。

  1. 您可以使用固定宽度整数写出字符串的字节长度,然后再写出实际字节。然后,读取器可以先读取长度,然后再读取后面指定的字节数:

    DataOutputStream dos = new DataOutputStream(outputStream);
    byte[] bytes;
    int len;

    bytes = username.getText().getBytes(StandardCharsets.UTF_8);
    len = bytes.length;
    dos.writeInt(len);
    dos.write(bytes, 0, len);

    bytes = password.getText().getBytes(StandardCharsets.UTF_8);
    len = bytes.length;
    dos.writeInt(len);
    dos.write(bytes, 0, len);

    inputStream = s.getInputStream();
    DataInputStream dis = new DataInputStream(inputStream);
    byte[] bytes;
    int len;

    len = dis.readInt();
    bytes = new byte[len];
    dis.readFully(bytes);
    String username = new String(bytes, StandardCharsets.UTF_8);

    len = dis.readInt();
    bytes = new byte[len];
    dis.readFully(bytes);
    String password = new String(bytes, StandardCharsets.UTF_8);

    或者,DataOutputStreamDataInputStream 可以直接写入/读取 String 值,在内部为您处理上述逻辑(使用 Short 而不是 int 来表示长度值):

    DataOutputStream dos = new DataOutputStream(outputStream);
    dos.writeUTF(username.getText());
    dos.writeUTF(password.getText());

    inputStream = s.getInputStream();
    DataInputStream dis = new DataInputStream(inputStream);

    String username = dis.readUTF();
    String password = dis.readUTF();
  2. 您可以写出一个永远不会出现在字符串值本身中的唯一字符序列,例如换行符或控制字符(甚至是空终止符)。然后,读取器可以读取字节,直到遇到该序列:

    OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); 
    String s;

    s = username.getText();
    writer.write(s, 0, s.length());
    writer.write(10);

    s = password.getText();
    writer.write(s, 0, s.length());
    writer.write(10);

    inputStream = s.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));

    String username = reader.readLine();
    String password = reader.readLine();

    或者:

    OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8); 
    String s;

    s = username.getText();
    writer.write(s, 0, s.length());
    writer.write(0);

    s = password.getText();
    writer.write(s, 0, s.length());
    writer.write(0);

    inputStream = s.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
    StringBuilder sb = new StringBuilder();
    int ch;

    do
    {
    ch = reader.read();
    if (ch <= 0) break;
    sb.append((char)ch);
    }
    while (true);
    String username = sb.toString();

    sb.setLength(0);
    do
    {
    ch = reader.read();
    if (ch <= 0) break;
    sb.append((char)ch);
    }
    while (true);
    String password = sb.toString();

关于Java - 发送到 OutputStream 的两个字符串之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37217378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com