gpt4 book ai didi

java - 获取字符串中的bytedata数据

转载 作者:行者123 更新时间:2023-11-30 03:29:44 25 4
gpt4 key购买 nike

private static int readAndWriteInputStream( final InputStream is, final OutputStream outStream ) throws IOException {
final byte[] buf = new byte[ 8192 ];
int read = 0;
int cntRead;
while ( ( cntRead = is.read( buf, 0, buf.length ) ) >=0 )
{
outStream.write(buf, 0, cntRead);
read += cntRead;
}
outStream.write("\n".getBytes());
return read;
}

outStream.write(buf, 0, cntRead) 之前;我想将每一行(从输入文本读取)文件放入字符串中。是否可以将此字节数据放入字符串中。

最佳答案

更好的方法是使用 proper String constructor :

String s = new String(buf, 0, cntRead);

这样可以避免不必要的数组复制。

此外,如果数据编码可能与您平台的默认编码不同,您必须使用 a constructor它采用 Charset 作为附加参数。

关于java - 获取字符串中的bytedata数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29350079/

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