gpt4 book ai didi

java - 将输出膨胀到文件性能改进

转载 作者:行者123 更新时间:2023-12-02 13:37:59 25 4
gpt4 key购买 nike

我正在执行类似于下面的代码。环顾不同的实现,似乎大多数人都通过字节复制来执行相同的操作。是否有可能有一种更快的方法来处理文件的膨胀并打印回文件?

public static String unzipString(InputStream in) {
try {
int length = (int) in.readUBits( 16 );
// Add extra byte to array when Inflater is set to true
byte[] data = in.read( length );

ByteArrayInputStream bin = new ByteArrayInputStream(input);
InflaterInputStream in = new InflaterInputStream(bin);
FileoutputStream bout = new FileoutputStream(this.file);
int b;
while ((b = in.read()) != -1) {
bout.write(b);
}
bout.close();

} catch (IOException io) {
return null;
}

}

最佳答案

一次复制一个字节总是一种非常慢的处理文件的方式。我建议您改用 8 KB 的缓冲区。

try (FileOutputStream fout = new FileOutputStream(this.file)) {
byte[] bytes = new byte[8192];
for (int len; (len = in.read(bytes)) != -1;)
fout.write(b, 0, len);
}

顺便说一句,为了使其更快,您可以避免首先使用 InputStream 复制 byte[],InputStream 包装 in 但准确读取 length 字节。

关于java - 将输出膨胀到文件性能改进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42887352/

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