GJava中从标准输入到标准输出的压缩

发布于 2024-12-12 10:20:29 字数 562 浏览 1 评论 0 原文

前言:我是一个彻头彻尾的Java菜鸟...我昨天刚刚写了Hello World。怜悯我的菜鸟自己。

我不知道如何在 Java 中从标准输入或输出读取到标准输出。我知道有像 Scanners 和 System.out.println 这样的东西,但这似乎并不直接适用于我想要做的事情。

特别是,我尝试在标准输入上使用 GZip 并将压缩结果输出到标准输出。我看到有 GZipOutputStream 类 我肯定会想使用。但是,如何初始化输出流以定向到 std 输出?此外,我怎样才能从标准输入中读取?

我怎样才能做到这一点?如何压缩 std 输入并将结果输出到 std 输出?

(这是我想要完成的任务的图表:Std 输入 -> GZIP(通过我的 Java 程序) -> std 输出(std 输入的压缩版本)

Preface: I'm a total Java noob...I just wrote Hello World yesterday. Have mercy on my noob self.

I'm not sure how to read from standard input or output to standard output in Java. I know there are things like Scanners and System.out.println, but this doesn't seem to apply directly to what I'm trying to do.

In particular, I'm trying to use GZip on standard input and output the compressed result to standard output. I see that there is a GZipOutputStream class that I'll certainly want to use. However, how can I initialize the output stream to direct to std output? Further, how can I just read from standard input?

How can I accomplish this? How do I compress std input and output the result to std output?

(Here's a diagram of what I'm trying to accomplish: Std input -> GZIP (via my Java program) -> std output (the compressed version of the std input)

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

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

发布评论

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

评论(2

停滞 2024-12-19 10:20:29

看一下下面的构造函数:GZIPInputStream(InputStream in)。要将标准输入作为InputStream,请使用System.in。从流中读取数据是使用 read(byte[] buf, int off, int len) 方法完成的 - 请查看文档以获取详细说明。
整个事情就像就我

GZIPInputStream i = new GZIPInputStream(System.in);
byte[] buffer = new byte[1024];
int n = i.read(buffer, 0,buffer.length)
System.out.println("Bytes read: " + n);

个人而言,我发现 Java 中的流有一个陡峭的学习曲线,所以我建议阅读任何在线教程。

我将把它作为练习来找出输出。

--
免责声明:尚未实际尝试过该代码

Take a look at the following constructor : GZIPInputStream(InputStream in). To get stdin as an InputStream, use System.in. Reading from the stream is done with the read(byte[] buf, int off, int len) method- take a look at the documentation for a detailed description.
The whole thing would be something like

GZIPInputStream i = new GZIPInputStream(System.in);
byte[] buffer = new byte[1024];
int n = i.read(buffer, 0,buffer.length)
System.out.println("Bytes read: " + n);

Personally, I found streams in Java to have a steep learning curve, so I do recommend reading any tutorial online.

I'll leave it as an exercise to figure out the output.

--
Disclaimer: haven't actually tried the code

究竟谁懂我的在乎 2024-12-19 10:20:29
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class InToGzipOut {
    private static final int BUFFER_SIZE = 512;

    public static void main(String[] args) throws IOException {
        byte[] buf = new byte[BUFFER_SIZE];
        GZIPOutputStream out = new GZIPOutputStream(System.out);
        int len;
        while ((len = System.in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.finish();

    }
}
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class InToGzipOut {
    private static final int BUFFER_SIZE = 512;

    public static void main(String[] args) throws IOException {
        byte[] buf = new byte[BUFFER_SIZE];
        GZIPOutputStream out = new GZIPOutputStream(System.out);
        int len;
        while ((len = System.in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.finish();

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