java String

Java.IO.File compareTo() Compareto in Java Code With Examples

Table of Contents

Program

import java.io.File;
public class FileCompareToPathName
{
	public static void main(String[] args) 
	{
		File firstFile = new File("Jaswanth1");
		File secondFile = new File("Jaswanth2");
		int i = firstFile.compareTo(secondFile);
		if (i == 0) 
		{
			System.out.println("they are equal");
		}
		if (i > 0) 
		{
			System.out.println("First file is greater than the second file");
		}
		if (i < 0)
		{
			System.out.println("Second file is greater than the first file");
		}
	}
}

Output

Second file is greater than the first file

Description

public int compareTo​(File pathname)

Compares two abstract pathnames lexicographically. The ordering defined by this method depends upon the underlying system. On UNIX systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows systems it is not.

Specified by:

compareTo in interface Comparable

Parameters:

pathname – The abstract pathname to be compared to this abstract pathname

Returns:

Zero if the argument is equal to this abstract pathname, a value less than zero if this abstract pathname is lexicographically less than the argument, or a value greater than zero if this abstract pathname is lexicographically greater than the argument