2009年2月18日 星期三

第155題

The doesFileExist method takes an array of directory names representing a path from the root filesystem and a file name. The method returns true if the file exists, false if it does not.
Place the code fragments in position to complete this method.

public static boolean doesFileExist(String[] directories, String filename){
Place here
for(String dir : directories){
Place here
}
Place here
Place here

}

Code Fragments
path = path.getSubdirectory(dir);
return !file.isNew();
return(file != null);
String path = "";
path = path.getFile(filename);
File path = new File("");
return file.exists();
return path.isFile();
File file = new File(path, filename);
path = new File(path, dir);
File path = new File(File.separator);
path = path + File.separator + dir;

答案:
public static boolean doesFileExist(String[] directories, String filename){
String path = "";
for(String dir : directories){
path = path + File.separator + dir;
}
File file = new File(path, filename);
return file.exists();
}
參考:10-3 File類別

沒有留言:

張貼留言