[英]redirect output to text file after user input
I have created a JNDI java program that gives different kind of output as user specification. 我创建了一个JNDI java程序,该程序提供不同种类的输出作为用户规范。 For example user can run the program with different parameters as :
例如,用户可以使用以下不同参数运行程序:
java MyProg -o row -u username -l uId1,uId2,....
here user can specify options as : -o : for output format(row basis/column basis) -u : connect to server with different user(then program will ask for password) -l : program will show output for given user id(s) 在此用户可以将选项指定为:-o:用于输出格式(行/列)-u:以其他用户身份连接到服务器(程序将要求输入密码)-l:程序将显示给定用户ID的输出)
Now the thing is for every option I have define a default value(configuration) so its up to user if he want to change the configuration then he will specify the option. 现在,对于每个选项,我都定义了一个默认值(配置),因此如果用户要更改配置,则取决于用户,然后他将指定该选项。 So user can call the program as:
因此,用户可以通过以下方式调用程序:
java MyProg
java Myprog -o col
java MyProg -u username
java MyProg -l uId1,uId2,...
java MyProg -l uId1,uId2,... -o col
and so on.. 等等..
so whenever user use -u(for changing the username) then the program will ask the password after that it shows the result. 因此,每当用户使用-u(用于更改用户名)时,程序都会在询问密码后显示结果。
Now I want to provide a facility that user can redirect the console output to a text file but when I am trying "java MyProg > filename.txt" then its not working. 现在,我想提供一种工具,用户可以将控制台输出重定向到文本文件,但是当我尝试“ java MyProg> filename.txt”时,它不起作用。
Please tell me how to redirect the output to a file. 请告诉我如何将输出重定向到文件。
System.out
is a PrintStream (which is why you can use such methods as println()
etc on it). System.out
是一个PrintStream (这就是为什么您可以在其上使用诸如println()
等方法的原因)。
What you can do is, if you see the option to redirect to a file, just create a new PrintStream
to the destination file: 您可以做的是,如果看到重定向到文件的选项,只需创建一个新的
PrintStream
到目标文件:
PrintStream stdout = System.out;
// outfile is a String
if (outfile != null)
stdout = new PrintStream(outfile);
And use stdout
instead of System.out
to output results. 并使用
stdout
而不是System.out
输出结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.