I'm writing a program that expects an argument which can be both a file and a normal String argument. If it is a file, then I want to read the content of the file, if it is a String, I want to read the String.
我正在编写一个程序,它需要一个可以是文件和普通String参数的参数。如果是文件,那么我想读取文件的内容,如果是String,我想读取String。
The only idea I came up with is doing something like:
我想出的唯一想法就是:
File file = new File(args[0]);
and then checking whether if ( !file.isDirectory() || !file.isFile() )
and if that's the case, treating args[0]
as a String. But if args[0]
is a non existing file, like C:/Users/Name/Desktop/nonexistingFile.txt, it treats this argument like a String: "C:/Users/Name/Desktop/nonexistingFile.txt" to work with instead of saying "it's a non existing file".
然后检查是否(!file.isDirectory()||!file.isFile())以及是否是这种情况,将args [0]视为String。但是如果args [0]是一个非现有文件,比如C:/Users/Name/Desktop/nonexistingFile.txt,它会将此参数视为字符串:“C:/Users/Name/Desktop/nonexistingFile.txt”工作而不是说“这是一个不存在的文件”。
How to check if args[0]
is a normal String and not a non existing file?
如何检查args [0]是普通字符串而不是非现有文件?
1 个解决方案
#1
2
If with args
you are referring to the String array of your main
method, then it is guaranteed to contains no null
and every single element is a (proper) String
.
如果使用args引用main方法的String数组,则保证不包含null,并且每个元素都是(正确的)String。
However you would need to check whether args
length is at least 1 as there is no guarantee that the user would had provided any argument:
但是,您需要检查args长度是否至少为1,因为无法保证用户提供任何参数:
public static void main(String[] args) {
if (args.length <= 0) {
// complain no argument was provided?
return;
}
final String myFileName = args[0];
final File myFile = new File(myFileName);
if (!myFile.exists()) {
// complain is not an existing file
} else if (myFile.isFile()) {
// handle the case the input file is a regular file.
} else if (myFile.isDirectory()) {
// handle the case the input file is in fact a directory
} else {
// complain is neither.
// some operating system may contain other types of devices that are neither
// files nor directories. so you should deal with such cases gracefully.
}
}
However, since in practice some other piece of code may call that main
method, not just the VM's start-up code perhaps you wanna do the actual check that args
is in fact not null nor does it contain null
(at least not in args[0]
):
但是,因为在实践中,其他一些代码可能会调用该main方法,而不仅仅是VM的启动代码,或许您想要进行实际检查,即args实际上不是null,也不包含null(至少不在args中[ 0]):
public static void main(String[] args) {
if (args == null) {
// complain about this unexpected input (return or throw NPE, IAE???)
throw new IllegalArgumentException(); // in this case IAE.
} else if (args.length <= 0) {
// complain no argument was provided?
return;
} else if (args[0] == null) {
// complain about this unexpected input (return or throw NPE, IAE???)
throw new NullPointerException(); // in this case NPE.
}
...
#1
2
If with args
you are referring to the String array of your main
method, then it is guaranteed to contains no null
and every single element is a (proper) String
.
如果使用args引用main方法的String数组,则保证不包含null,并且每个元素都是(正确的)String。
However you would need to check whether args
length is at least 1 as there is no guarantee that the user would had provided any argument:
但是,您需要检查args长度是否至少为1,因为无法保证用户提供任何参数:
public static void main(String[] args) {
if (args.length <= 0) {
// complain no argument was provided?
return;
}
final String myFileName = args[0];
final File myFile = new File(myFileName);
if (!myFile.exists()) {
// complain is not an existing file
} else if (myFile.isFile()) {
// handle the case the input file is a regular file.
} else if (myFile.isDirectory()) {
// handle the case the input file is in fact a directory
} else {
// complain is neither.
// some operating system may contain other types of devices that are neither
// files nor directories. so you should deal with such cases gracefully.
}
}
However, since in practice some other piece of code may call that main
method, not just the VM's start-up code perhaps you wanna do the actual check that args
is in fact not null nor does it contain null
(at least not in args[0]
):
但是,因为在实践中,其他一些代码可能会调用该main方法,而不仅仅是VM的启动代码,或许您想要进行实际检查,即args实际上不是null,也不包含null(至少不在args中[ 0]):
public static void main(String[] args) {
if (args == null) {
// complain about this unexpected input (return or throw NPE, IAE???)
throw new IllegalArgumentException(); // in this case IAE.
} else if (args.length <= 0) {
// complain no argument was provided?
return;
} else if (args[0] == null) {
// complain about this unexpected input (return or throw NPE, IAE???)
throw new NullPointerException(); // in this case NPE.
}
...