Given the directory structure that contains three directories: company, Salesdat, and Finance:
Company
– Salesdat
– Target.dat
– Finance
– Salary.dat
– Annual.dat
And the code fragment:
If Company is the current directory, what is the result?
A.
Prints only Annual.dat
B.
Prints only Salesdat, Annual.dat
C.
Prints only Annual.dat, Salary.dat, Target.dat
D.
Prints at least Salesdat, Annual.dat, Salary.dat, Target.dat
Explanation:
The pattern *dat will matchthe directory name Salesdat and it will also match the file Annual.dat.
It will not be matched to Target.dat which is in a subdirectory.
C is correct. B is *****t
——————
create directories and compile it:
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import static java.nio.file.FileVisitResult.*;
public class Frage134 extends SimpleFileVisitor {
private final PathMatcher matcher;
Frage134(){
matcher = FileSystems.getDefault().getPathMatcher(“glob:*dat”);
}
void find(Path file){
Path name = file.getFileName();
if(name != null && matcher.matches(name)){
System.out.println(name);
}
}
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs){
find(file);
return CONTINUE;
}
public static void main(String[] args) throws IOException {
Frage134 obj = new Frage134();
Files.walkFileTree(Paths.get(“c:/company”), obj);
}
}
Tested , if based on the assumption that the other .dat files are in seperated folders, then A, i.e., folder is not counted in.
Company
Salesdat
– Target.dat
Finance
– Salary.dat
Annual.dat
Tested and the answer is C. All the .dat files including the ones in subfolds will be printed.
+1, because we are using walkFileTree() so all sub-directories are traversed. However since we only override visitFile(), only files are found by the find() method.
Answer is D
Because the pattern is *dat not *.dat
All files ending with dat (not .dat)
then the output :
Salesdat
Target.dat
Salary.dat
Annual.dat
certainly not B or D
The answer is C, because Company contains to Finance(Salary.dat and Annual.dat) and Salesdat(Target.dat). Seeing the perspective of business.
C
The answer is C, because it writes the name of files and we are using walkFileTree() so all sub-directories are traversed. if you want to write the name of directory then you must use the method:
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
System.out.println(“pre: ” + dir);
return FileVisitResult.CONTINUE; }
c
+1