Here is a fraction of my code.
这是我的代码的一小部分。
if(args.length != 2) {
System.out.println("Usage: copyFile from to");
return;
}
The book I'm studying says that the code above is for:
我正在研究的这本书说上面的代码是:
making sure that a file name has been specified
确保指定了文件名
I don't understand why there is 2 in if
does args.length != 2
make sure that the file name has been specified?
我不明白为什么在args.length中有2个!= 2确保已经指定了文件名?
I can't understand. Please help me out. And a little bit of detailed help could be a lot of use for me. Thanks.
我无法理解。请帮帮我。对我来说,一点点详细的帮助可能会有很多用处。谢谢。
2 个解决方案
#1
The usage in your code clearly mentions that
代码中的用法清楚地提到了这一点
System.out.println("Usage: copyFile from to");
So, your code needs to check whether two filenames are passed to it or not!
因此,您的代码需要检查是否传递了两个文件名!
First argument passed to the command-line should be SourceFile/Directory and the second argument should be DestinationFile/Directory where first one is to be copied.
传递给命令行的第一个参数应该是SourceFile / Directory,第二个参数应该是DestinationFile / Directory,其中第一个参数将被复制。
hence,
if(args.length != 2) {
System.out.println("Usage: copyFile from to");
return;
}
and the code should be run as :-
并且代码应该运行为: -
java SourceClassName /path/of/source/file(folder) /path/of/destination/file(folder)
#2
The variable args
hold the input to main method in Java class. In Java args
contains the supplied command-line arguments as an array of String objects.
变量args保存Java类中main方法的输入。在Java中,args包含提供的命令行参数作为String对象的数组。
For example running your class this way: java yourProgram input1 input2
then args = {"input1", "input2"}
例如以这种方式运行你的类:java yourProgram input1 input2 then args = {“input1”,“input2”}
Acording to your code snipet this if is checking that there are num of inputs different to 2. If it's true, print the use message and return. That's all I can say: without more code I can't say why this if is checking what you say but this is how args
variable works.
根据你的代码snipet这是否检查有多个输入不同于2.如果是,则打印使用消息并返回。这就是我所能说的:没有更多的代码,我不能说为什么这会检查你说的是什么,但这就是args变量的工作方式。
I guess your main method is expecting two inputs, maybe the first one the filename you want to copy from and the second one the filename of the file you want to copy to.
我猜您的主要方法是期望两个输入,可能是第一个要复制的文件名,第二个是要复制到的文件的文件名。
#1
The usage in your code clearly mentions that
代码中的用法清楚地提到了这一点
System.out.println("Usage: copyFile from to");
So, your code needs to check whether two filenames are passed to it or not!
因此,您的代码需要检查是否传递了两个文件名!
First argument passed to the command-line should be SourceFile/Directory and the second argument should be DestinationFile/Directory where first one is to be copied.
传递给命令行的第一个参数应该是SourceFile / Directory,第二个参数应该是DestinationFile / Directory,其中第一个参数将被复制。
hence,
if(args.length != 2) {
System.out.println("Usage: copyFile from to");
return;
}
and the code should be run as :-
并且代码应该运行为: -
java SourceClassName /path/of/source/file(folder) /path/of/destination/file(folder)
#2
The variable args
hold the input to main method in Java class. In Java args
contains the supplied command-line arguments as an array of String objects.
变量args保存Java类中main方法的输入。在Java中,args包含提供的命令行参数作为String对象的数组。
For example running your class this way: java yourProgram input1 input2
then args = {"input1", "input2"}
例如以这种方式运行你的类:java yourProgram input1 input2 then args = {“input1”,“input2”}
Acording to your code snipet this if is checking that there are num of inputs different to 2. If it's true, print the use message and return. That's all I can say: without more code I can't say why this if is checking what you say but this is how args
variable works.
根据你的代码snipet这是否检查有多个输入不同于2.如果是,则打印使用消息并返回。这就是我所能说的:没有更多的代码,我不能说为什么这会检查你说的是什么,但这就是args变量的工作方式。
I guess your main method is expecting two inputs, maybe the first one the filename you want to copy from and the second one the filename of the file you want to copy to.
我猜您的主要方法是期望两个输入,可能是第一个要复制的文件名,第二个是要复制到的文件的文件名。