martedì 5 novembre 2013

Q30

Given the code fragment:

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;
 

public class IsContentSame {
     public static boolean isContentSame() throws IOException {
          Path p1 = Paths.get("D:\\faculty\\report.txt");
          Path p2 = Paths.get("C:\\student\\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());
          }
     }
}

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.


Risposta  B

Infatti pur rimpiazzando il secondo file con il primo isSameFile  controlla se due path puntano allo stesso unico file e non se due file diversi sono uguali nel contenuto. Quindi il ritorno di IsContentSame è false.

Riferimenti:

http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html

Nessun commento:

Posta un commento