java file pathname_int compareTo(File pathname)
描述
所述java.io.File.compareTo(File pathname)方法按字母順序比較兩個抽象路徑名。此方法定義的順序取決于操作系統。
聲明
以下是java.io.File.compareTo(File pathname)方法的聲明
public int compareTo(File pathname)
參數
pathname - 要與此抽象路徑名進行比較的抽象路徑名。
返回值
如果參數等于此抽象路徑名,則返回零,如果抽象路徑名在字典上小于參數且大于參數,則該方法返回大于0的值。
異常
NA
實例
以下示例顯示了java.io.File.compareTo(File pathname)方法的用法。
package com.tutorialspoint;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
File f1 = null;
try {
// create new files
f = new File("test.txt");
f1 = new File("File/test1.txt");
// returns integer value
int value = f.compareTo(f1);
// prints
System.out.print("Lexicographically, ");
System.out.print("abstract path name test.txt");
// if lexicographically, argument = abstract path name
if(value == 0) {
System.out.print(" = ");
}
// if lexicographically, argument < abstract path name
else if(value > 0) {
System.out.print(" > ");
}
// if lexicographically, the argument > abstract path name
else {
System.out.print(" < ");
}
System.out.println("abstract path name File/test1.txt");
// prints the value returned by compareTo()
System.out.print("Value returned: "+value);
} catch(Exception e) {
e.printStackTrace();
}
}
}
讓我們編譯并運行上面的程序,這將產生以下結果
Lexicographically, abstract path name test.txt > abstract path name File/test1.txt
Value returned: 46
總結
以上是生活随笔為你收集整理的java file pathname_int compareTo(File pathname)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java音乐播放器文库_android音
- 下一篇: potplayer 多个进程_进程组、会