OnJava8-Examples/files/Directories.java

95 lines
2.5 KiB
Java
Raw Normal View History

2015-12-06 11:45:16 -08:00
// files/Directories.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-12-06 11:45:16 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-12-06 11:45:16 -08:00
import java.util.*;
import java.nio.file.*;
import onjava.RmDir;
public class Directories {
static Path test = Paths.get("test");
static String sep =
FileSystems.getDefault().getSeparator();
static List<String> parts =
2015-12-15 11:47:04 -08:00
Arrays.asList("foo", "bar", "baz", "bag");
2015-12-06 11:45:16 -08:00
static Path makeVariant() {
Collections.rotate(parts, 1);
return Paths.get("test", String.join(sep, parts));
}
static void refreshTestDir() throws Exception {
if(Files.exists(test))
RmDir.rmdir(test);
if(!Files.exists(test))
Files.createDirectory(test);
}
2016-01-25 18:05:55 -08:00
public static void
main(String[] args) throws Exception {
2015-12-06 11:45:16 -08:00
refreshTestDir();
Files.createFile(test.resolve("Hello.txt"));
Path variant = makeVariant();
// Throws exception (too many levels):
try {
Files.createDirectory(variant);
} catch(Exception e) {
System.out.println("Nope, that doesn't work.");
}
populateTestDir();
Path tempdir =
2015-12-15 11:47:04 -08:00
Files.createTempDirectory(test, "DIR_");
Files.createTempFile(tempdir, "pre", ".non");
2015-12-06 11:45:16 -08:00
Files.newDirectoryStream(test)
.forEach(System.out::println);
System.out.println("*********");
Files.walk(test).forEach(System.out::println);
}
static void populateTestDir() throws Exception {
for(int i = 0; i < parts.size(); i++) {
Path variant = makeVariant();
if(!Files.exists(variant)) {
Files.createDirectories(variant);
Files.copy(Paths.get("Directories.java"),
variant.resolve("File.txt"));
Files.createTempFile(variant, null, null);
}
}
}
}
/* Output:
Nope, that doesn't work.
2015-12-15 11:47:04 -08:00
test\bag
2015-12-06 11:45:16 -08:00
test\bar
test\baz
2016-07-27 11:12:11 -06:00
test\DIR_3697011820450773773
2015-12-06 11:45:16 -08:00
test\foo
test\Hello.txt
*********
test
2015-12-15 11:47:04 -08:00
test\bag
test\bag\foo
test\bag\foo\bar
test\bag\foo\bar\baz
2016-07-27 11:12:11 -06:00
test\bag\foo\bar\baz\234245189903117886.tmp
2015-12-15 11:47:04 -08:00
test\bag\foo\bar\baz\File.txt
2015-12-06 11:45:16 -08:00
test\bar
test\bar\baz
2015-12-15 11:47:04 -08:00
test\bar\baz\bag
test\bar\baz\bag\foo
2016-07-27 11:12:11 -06:00
test\bar\baz\bag\foo\8752987828022262887.tmp
2015-12-15 11:47:04 -08:00
test\bar\baz\bag\foo\File.txt
2015-12-06 11:45:16 -08:00
test\baz
2015-12-15 11:47:04 -08:00
test\baz\bag
test\baz\bag\foo
test\baz\bag\foo\bar
2016-07-27 11:12:11 -06:00
test\baz\bag\foo\bar\465343347528104633.tmp
2015-12-15 11:47:04 -08:00
test\baz\bag\foo\bar\File.txt
2016-07-27 11:12:11 -06:00
test\DIR_3697011820450773773
test\DIR_3697011820450773773\pre3304897437567097054.non
2015-12-06 11:45:16 -08:00
test\foo
test\foo\bar
test\foo\bar\baz
2015-12-15 11:47:04 -08:00
test\foo\bar\baz\bag
2016-07-27 11:12:11 -06:00
test\foo\bar\baz\bag\2118206626074637804.tmp
2015-12-15 11:47:04 -08:00
test\foo\bar\baz\bag\File.txt
2015-12-06 11:45:16 -08:00
test\Hello.txt
*/