What is the result when the result.txt file already exists in c:\student?
A.
The program replaces the file contents and the file’s attributes and prints Equal.
B.
The program replaces the file contents as well as the file attributes and prints Not equal.
C.
An UnsupportedOperationException is thrown at runtime.
D.
The program replaces only the file attributes and prints Not equal.
Explanation:
Assuming there is a file D:\\faculty\\report.txt then this file will be copied and will be replacing
C:\\student\\report.txt.
B
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
/**
*
* @author Humberto
*/
public class IsContentSame {
public static boolean isContentSame() throws IOException {
Path p1 = Paths.get(“C:\\java7\\faculty\\report.txt”);
Path p2 = Paths.get(“C:\\java7\\student\\report.txt”);
//Path p2 = Paths.get(“C:\\java7\\faculty\\report.txt”);
Files.copy(p1, p2, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES, LinkOption.NOFOLLOW_LINKS);
if (Files.isSameFile(p1, p2)) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
try {
boolean flag = isContentSame();
if (flag) {
System.out.println(“Equal”);
} else {
System.out.println(“Not equal”);
}
} catch (IOException e) {
System.err.println(“Caught IOException: ” + e.getMessage());
}
}
}