Java File.compareTo(File pathname)
方法具有以下語法。
public int compareTo(File pathname)
在下面的程式碼顯示如何使用File.compareTo(File pathname)
方法。
import java.io.File;
public class Main {
public static void main(String[] args) {
File f = new File("test.txt");
File f1 = new File("File/test1.txt");
// returns integer value
int value = f.compareTo(f1);
if (value == 0) {
System.out.print(" = ");
}
else if (value > 0) {
System.out.print(" > ");
}
else {
System.out.print(" < ");
}
System.out.print("Value returned: " + value);
}
}
上面程式碼執行結果如下 -
> Value returned: 14