用于零终止字符串的 Java BufferedReader

发布于 2024-08-10 10:54:14 字数 100 浏览 9 评论 0原文

我需要从 Java 中的 InputStream 读取以零结尾的字符串。

是否有类似于 BufferedReader.readLine() 的方法来读取以零结尾的字符串?

I need to read zero-terminated strings from InputStream in Java.

Is there similar to BufferedReader.readLine() method for reading zero-terminated strings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城°AllureLove 2024-08-17 10:54:14
package com;

import java.io.*;
import java.util.Scanner;

public class AAA {

    private static final String ENCODING = "UTF-8";

    public static void main(String[] args) throws Exception {
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        bOut.write("the first line".getBytes(ENCODING));
        bOut.write(0);
        bOut.write("the second line\r\n (long one)".getBytes(ENCODING));
        bOut.write(0);
        bOut.write("the third line".getBytes(ENCODING));
        printLines(new ByteArrayInputStream(bOut.toByteArray()));
    }

    public static void printLines(InputStream in) {
        Scanner scanner = new Scanner(in, ENCODING);
        scanner.useDelimiter("\u0000");
        while (scanner.hasNext()) {
            System.out.println(scanner.next());
            System.out.println("--------------------------------");
        }
    }
}
package com;

import java.io.*;
import java.util.Scanner;

public class AAA {

    private static final String ENCODING = "UTF-8";

    public static void main(String[] args) throws Exception {
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        bOut.write("the first line".getBytes(ENCODING));
        bOut.write(0);
        bOut.write("the second line\r\n (long one)".getBytes(ENCODING));
        bOut.write(0);
        bOut.write("the third line".getBytes(ENCODING));
        printLines(new ByteArrayInputStream(bOut.toByteArray()));
    }

    public static void printLines(InputStream in) {
        Scanner scanner = new Scanner(in, ENCODING);
        scanner.useDelimiter("\u0000");
        while (scanner.hasNext()) {
            System.out.println(scanner.next());
            System.out.println("--------------------------------");
        }
    }
}
不美如何 2024-08-17 10:54:14

不可以。Java 本身不识别以零结尾的字符串。您必须读取 InputStream 并查找 0 字节。

请注意,这并没有解决字符编码的问题。 InputStream 将为您提供字节流,然后您必须通过 Reader 编码为字符。如果您有多字节字符编码,那么问题会变得更加复杂。

No. Java doesn't recognise a zero-terminated string as such. You'll have to read the InputStream and look for a 0 byte.

Note that this doesn't address the issue of character-encoding. The InputStream will give you the stream of bytes, and you'll then have to encode to characters via a Reader. If you have a multi-byte character encoding then the issue becomes more complex.

贪恋 2024-08-17 10:54:14

您还需要了解“零”意味着什么。输入/输出流处理字节,而读取器/写入器处理字符。如果您想匹配零字符,则字节到字符转换编码将发挥作用。

you will also need to understand what is that "zero" means . input/output streams deal with bytes whereas readers/writers deal with characters. if you want to match against a zero character then byte to char conversion encoding will come into play.

小耗子 2024-08-17 10:54:14

您可以创建一种类似于下面的方法。从 InputStream 创建一个 BufferedReaderBufferedReader 通过引用传递,因此它将保留状态。它也可以很容易地存储在实例变量中。

public String readLine(BufferedReader buff) throws IOException{
    int c = buff.read();
    // if EOF
    if (c == -1){
        return null;
    }
    StringBuilder builder = new StringBuilder("");
    // Check if new line or EOF
    while (c != -1 && c != 0){
        builder.append((char) c);
        c = buff.read();
    }
    return builder.toString();
}

....

String line = reader.readLine(buff);
while (line != null)
{
System.out.println(line);
line = reader.readLine(someBufferedReader);

}

You could create a method similar to the one below. Create a BufferedReader from an InputStream. The BufferedReader is passed by reference so it will retain state. It could easily be stored in an instance variable as well.

public String readLine(BufferedReader buff) throws IOException{
    int c = buff.read();
    // if EOF
    if (c == -1){
        return null;
    }
    StringBuilder builder = new StringBuilder("");
    // Check if new line or EOF
    while (c != -1 && c != 0){
        builder.append((char) c);
        c = buff.read();
    }
    return builder.toString();
}

....

String line = reader.readLine(buff);
while (line != null)
{
System.out.println(line);
line = reader.readLine(someBufferedReader);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文