前言:我是一个彻头彻尾的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)
发布评论
评论(2)
看一下下面的构造函数:
GZIPInputStream(InputStream in)
。要将标准输入作为InputStream
,请使用System.in
。从流中读取数据是使用 read(byte[] buf, int off, int len) 方法完成的 - 请查看文档以获取详细说明。整个事情就像就我
个人而言,我发现 Java 中的流有一个陡峭的学习曲线,所以我建议阅读任何在线教程。
我将把它作为练习来找出输出。
--
免责声明:尚未实际尝试过该代码
Take a look at the following constructor :
GZIPInputStream(InputStream in)
. To get stdin as anInputStream
, useSystem.in
. Reading from the stream is done with theread(byte[] buf, int off, int len)
method- take a look at the documentation for a detailed description.The whole thing would be something like
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