Java : How to program. Tenth edition. Early objects Question 17.10 (Summarizing
ID: 3684277 • Letter: J
Question
Java : How to program. Tenth edition. Early objects
Question 17.10
(Summarizing the file types in a directory). Section 15.3 demonstrated how to get information about files and directories on disk. In addition, you used a DirectoryStream to display the contents of a directory. Interface DirectoryStream now contains default method entries, which returns a Stream. Use the techniques from Seciton 15.3, DirectoryStream method entries, lambdas and streams to summarize the types of files in a specified directory.
Explanation / Answer
class disp_filetypes
{
public static void main(String args[])
{
Path dir ="C:mydir";
List<Path> listSourceFiles(Path dir) throws IOException {
List<Path> result = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{c,h,cpp,hpp,java}")) {
for (Path entry: stream) {
result.add(entry);
}
} catch (DirectoryIteratorException ex) {
// I/O error encounted during the iteration, the cause is an IOException
throw ex.getCause();
}
for(i=0;i<result.length();i++)
System.out.println(result[i];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.