重定向和管道 [英] redirecting and piping

查看:87
本文介绍了重定向和管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以给我发送一段代码片段,说明如何使用输入文件在JAVA中使用它吗?
例如,我假设此代码放置在某处,因此可以调用一种方法并读取名为Average的文件中的值?该代码在哪里?

can some send me a code snippet on how to use input files to do work with in JAVA?
For instance I assume this code is placed somewhere so one can call a method and read the values in the file named Average? Where is this code located?

% java Average < data.txt
data.txt

推荐答案

请参阅我对该问题的评论:也许我听不懂这个问题,但我认为您只需要知道如何输入管道(<)起作用.

为了处理这种输入,您的应用程序只需要使用控制台输入,控制台输入本质上是一个称为标准输入"的流.您可以使用java.util.Scanner或输入流System.in来阅读它,请参阅:
http://www.java- tips.org/java-se-tips/java.util/how-to-read-input-from-console.html [ http://www.java2s.com/Tutorial/Java/0120__Development/Howtoreadfromstandardinput.htm [ ^ ].

当您以这种方式对输入进行编程并从不带管道的控制台运行应用程序时,它将接受操作员(用户)在控制台中键入的输入字符串.如果您按照问题所示在命令行中使用输入管道,则应用程序将假定所有输入字符串都按照操作员从控制台输入的相同顺序放置在输入文件"data.txt"中,带有代表[ENTER]键盘键的结束行字符(结束行,EOL标记).请注意,EOL的确切表示形式取决于系统:Windows上的CR LF,类Unix系统上的LF,Mac上的CR等,请参见 ^ ].基本上,在某些系统的文本编辑器中,使用EOL手动键入文本,保存数据文件并在同一系统中运行该应用程序,它将可以正常工作.

基本上,这就是您需要了解的所有内容.当然,您和您的用户应该可以在命令行模式下使用标准系统控制台或其他类似程序进行操作.

—SA
Please see my comment to the question: perhaps I failed to understand the question, but I think you just need to know how an input pipe (<) works.

To deal with this kind of input, your application just needs to work with console input, which is essentially a stream called "standard input". You can read it by using java.util.Scanner or input stream System.in, please see:
http://www.java-tips.org/java-se-tips/java.util/how-to-read-input-from-console.html[^],
http://www.java2s.com/Tutorial/Java/0120__Development/Howtoreadfromstandardinput.htm[^].

When you program your input like that and run the application from a console without a pipe, it will accept input strings typed by a human operator (user) in a console. If you use an input pipe as in your command line you''ve shown in your question, the application assume that all input strings are placed in your input file "data.txt" in the same order a human operator would enter from console, with end line characters (End-of-Line, EOL marker) representing [ENTER] keyboard key. Note that the exact presentation of EOL depends on the system: CR LF on Windows, LF on Unix-like systems, CR on Mac, etc., see http://en.wikipedia.org/wiki/New_line_character[^]. Basically, in you manually type the text with EOLs in a text editor in some system, save the data file and run the application in the same system, it will work correctly.

Basically, this is all you need to know. Of course, you and your users should be able to work in command-line mode with standard system console or other similar program.

—SA


这篇关于重定向和管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆