package class5_2_PathsAndFiles;// Dr. Yoder. MSOE. 12 January 2017 import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.InputMismatchException; import java.util.Iterator; import java.util.Scanner; import java.util.stream.Stream; public class PathDriver { public static void main(String[] args) { // main(args); to get a StackOverflowError Scanner in = new Scanner(System.in); System.out.println("This is a text-based program that allows \n" + "you to explore the files on your computer."); String name; do { System.out.println("The files in your current directory are: "); Path path = Paths.get("./example"); // Path path = Paths.get("C:\\d\\MyDocs\\Documents\\msoe\\class\\17q2\\se1021\\cfarc"); // for(Path folder: path) { // System.out.println("folder = " + folder); // } System.out.println("path = " + path); System.out.println("Files.exists(path) = " + Files.exists(path)); System.out.println("Files.isDirectory() = " + Files.isDirectory(path)); System.out.println("Files.isRegularFile(path) = " + Files.isRegularFile(path)); try { System.out.println("Files.walk() = " + Files.walk(path)); Stream stream = Files.walk(path); Iterator iterator = stream.iterator(); while(iterator.hasNext()) { Path subThing = iterator.next(); System.out.println("subThing = " + subThing); } } catch (IOException e) { System.out.println("Could not walk directory. Something went wrong!"); // TODO: Can I recover? } // Other Throwables InputMismatchException e; StackOverflowError e2; System.out.println("Enter the name of the file or\n" + "directory to select (blank line to exit): "); name = in.nextLine(); Path chosenPath = Paths.get("./example/"+name); System.out.println("chosenPath = " + chosenPath); System.out.println("Files.isDirectory(path]) = " + Files.isDirectory(chosenPath)); System.out.println("Files.exists(chosenPath) = " + Files.exists(chosenPath)); System.out.println("Files.isRegularFile() = " + Files.isRegularFile(chosenPath)); if(Files.isRegularFile(chosenPath)) { System.out.println("Opening the file!"); try { Scanner fileScanner = new Scanner(chosenPath); while(fileScanner.hasNextLine()) { System.out.println(fileScanner.nextLine()); } } catch (IOException e3) { System.out.println("Could not open the file, even though it exists!"); // TODO: recover somehow? } } else { System.out.println("The chosen file is not a a regular file. Check the path?"); System.out.println("chosenPath that could not be opened = " + chosenPath); } } while (name != null && !name.isEmpty()); System.out.println("Thank you for using the text-based file explorer!"); } }