使用作为主要参数传递的文件初始化程序 (system.getProperty())
我必须用这一行执行我的程序:
java -DImport=data.txt -Din=input.dat -Dout=output1.dat main.Main
我一直在试图弄清楚如何做到这一点,但无法真正让它工作。我正在使用 Netbeans,因为我不太熟悉 unix 终端类路径等。
public static void main(String[] args) {
String fileIn;
fileIn = System.getProperty ("Import");
}
由于上一条语句的结果,fileIn 变为 null。
我只是从 -DImport=data.txt 开始,解决它后我会尝试其他的。
这部分已经完成了,谢谢。我将尝试将 stdin 设置为 -Din 文件而不是键盘。谢谢
我做了你说的cartoonfox,它打印 Import null 这意味着 fileIn 没有从 System.getProperty("Import"); 接收任何字符串;
我还收到此警告:
warning: [deprecation] readLine() in java.io.DataInputStream 已被弃用 行= dis.readLine();
我正在使用此页面中的代码: http://www.java-tips.org/java-se-tips/java.io/how-to-read-file-in-java.html 因为我不知道很多读者:(
I have to execute my program with this line:
java -DImport=data.txt -Din=input.dat -Dout=output1.dat main.Main
I've been trying to figure it out how to do this but can't really get it working. I'm using Netbeans since I dont really get along with unix terminal classpaths and so on.
public static void main(String[] args) {
String fileIn;
fileIn = System.getProperty ("Import");
}
fileIn is getting null as a result of the previous statement.
I'm just starting with -DImport=data.txt, after I resolve it I'll try the others.
That part is done, Thank you. I'll try setting stdin as -Din file instead of keyboard. Thanks
I did what you said cartoonfox, Its printing Import null which means fileIn isnt receiving any String from System.getProperty("Import");
I am also getting this warning:
warning: [deprecation] readLine() in java.io.DataInputStream has been deprecated
line = dis.readLine();
I'm using code from this page: http://www.java-tips.org/java-se-tips/java.io/how-to-read-file-in-java.html since I dont know much of readers :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你混淆了两个概念:
你的args将是:
在您的代码中,您将两者混合。删除线
它将起作用:)
I think you are confusing 2 concepts:
your args will be:
In your code you are mixing both. Remove the line
and it will work :)
删除 if (args.length == 1) ,因为您不是解析参数,而是设置系统属性。
程序参数像这样在主类之后
drop
if (args.length == 1)
as you are not parsing arguments, but setting system properties.Program arguments go after the main class like this
将 -Import=foo 视为将“Import”配置选项设置为“foo”值的一种方式。
只需删除 if 语句即可:
顺便说一句,我认为 Sun 选择了 -D (而不是 - 其他内容) )因为很多 C 编译器允许您使用 -D 在命令行上设置宏 - 这意味着它是在命令行上设置名为“常量”的一种方法...这与 Java 中的操作类似。
我不确定为什么你运行这个会得到 null,所以这是我编译并运行它的记录 - 带有输出。你将不得不看看你的内容之间的差异在这份文字记录中,我正在做的事情以及我正在做的事情:
Think of -Import=foo as a way of setting the "Import" configuration option to value "foo".
Just drop the if statement:
BTW I think Sun chose -D (as opposed to - something else) because lots of C compilers allow you to set a macro on the command line with -D - meaning it'd be a way of setting named "constants" on the command-line... which is similar to what it does in Java.
I'm not sure why you'd get null running this, so here's a transcript of me compiling it and running it - with output. You're going to have to look at the differences between what you're doing and what I'm doing in this transcript: