Create three files of any type you choosefor example wordpro
Create three files of any type you choose—for example, word-processing documents, spreadsheets, or pictures. Write an application that determines whether the first two files are located in the same folder as the third one. Test the program when the files are in the same folder and when they are not. Save the file as CompareFolders.java.
Solution
CompareFolders.java :: Note that you need to set the file path and names according to your computer.
import java.nio.file.*;
public class CompareFolders
{
public static void main(String[] args)
{
String file1_name = \"file1.txt\";
String file1_path = \"/home/akash/Desktop/chegg/java file path/\";
String file2_name = \"file2.txt\";
String file2_path = \"/home/akash/Desktop/chegg/java file path/\";
String file3_name = \"file3.txt\";
String file3_path = \"/home/akash/Desktop/chegg/java file path/\";
Path path = Paths.get(file3_path + file1_name);
if (Files.exists(path)) {
System.out.println(\"file1 exists in same folder where file 3 exists!\");
}
if (Files.notExists(path)) {
System.out.println(\"file1 Does not exists in same folder where file 3 exists!\");
}
path = Paths.get(file3_path + file2_name);
if (Files.exists(path)) {
System.out.println(\"file2 exists in same folder where file 3 exists!\");
}
if (Files.notExists(path)) {
System.out.println(\"file2 Does not exists in same folder where file 3 exists!\");
}
}
}
sample output:
file1 exists in same folder where file 3 exists!
file2 Does not exists in same folder where file 3 exists!
