gpt4 book ai didi

用于零终止字符串的 Java BufferedReader

转载 作者:行者123 更新时间:2023-12-04 20:47:42 26 4
gpt4 key购买 nike

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

有没有类似BufferedReader.readLine()的读取零结尾字符串的方法?

最佳答案

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("--------------------------------");
}
}
}

关于用于零终止字符串的 Java BufferedReader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1686789/

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