| 7 | import onjava.RmDir; |
| 8 | |
| 9 | public class Directories { |
| 10 | static Path test = Paths.get("test"); |
| 11 | static String sep = |
| 12 | FileSystems.getDefault().getSeparator(); |
| 13 | static List<String> parts = |
| 14 | Arrays.asList("foo", "bar", "baz", "bag"); |
| 15 | static Path makeVariant() { |
| 16 | Collections.rotate(parts, 1); |
| 17 | return Paths.get("test", String.join(sep, parts)); |
| 18 | } |
| 19 | static void refreshTestDir() throws Exception { |
| 20 | if(Files.exists(test)) |
| 21 | RmDir.rmdir(test); |
| 22 | if(!Files.exists(test)) |
| 23 | Files.createDirectory(test); |
| 24 | } |
| 25 | public static void |
| 26 | main(String[] args) throws Exception { |
| 27 | refreshTestDir(); |
| 28 | Files.createFile(test.resolve("Hello.txt")); |
| 29 | Path variant = makeVariant(); |
| 30 | // Throws exception (too many levels): |
| 31 | try { |
| 32 | Files.createDirectory(variant); |
| 33 | } catch(Exception e) { |
| 34 | System.out.println("Nope, that doesn't work."); |
| 35 | } |
| 36 | populateTestDir(); |
| 37 | Path tempdir = |
| 38 | Files.createTempDirectory(test, "DIR_"); |
| 39 | Files.createTempFile(tempdir, "pre", ".non"); |
| 40 | Files.newDirectoryStream(test) |
| 41 | .forEach(System.out::println); |
| 42 | System.out.println("*********"); |
| 43 | Files.walk(test).forEach(System.out::println); |
| 44 | } |
| 45 | static void populateTestDir() throws Exception { |
| 46 | for(int i = 0; i < parts.size(); i++) { |
| 47 | Path variant = makeVariant(); |
| 48 | if(!Files.exists(variant)) { |
| 49 | Files.createDirectories(variant); |
| 50 | Files.copy(Paths.get("Directories.java"), |
| 51 | variant.resolve("File.txt")); |
| 52 | Files.createTempFile(variant, null, null); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | /* Output: |
| 58 | Nope, that doesn't work. |
| 59 | test\bag |