More Files
Methods
void delete(Path p)
This will delete an empty directory or a regular file sitting at the specified path. ANoSuchFileException
is thrown if the path points at a nonexistent item.void copy(Path source, Path dest, CopyOption... opts)
This will copy a file sitting atsource
to pathdest
.byte[] readAllBytes(Path p)
This will read all of the bytes into a byte array.String readString(Path p)
This will read a text file into a String.List<String> readAllLines(Path p)
This will read all of the lines of a file into a list of strings.Stream<Path> list(Path p)
This provides a stream of paths inp
ifp
is a directory and throws an exception otherwise.
What can you get a Stream
from?
Collection<T>
: Any class implementing this interface. Subinterfaces includeList<T>
andSet<T>
andQueue<T>
.ArrayList<T>
TreeSet<T>
HashSet<T>
Arrays.stream
will return a stream of the array's elements.
Also, a BufferedReader
has a lines()
method
that can offer you a Stream<String>
.
Files.list
will give you a stream of a directory's contents.
Filtering Streams
There is an interface Predicate<T>
.
java Crawler directory extension directory: a directory extension: file extension Show all files having the extension living in the directory.Print out all lines in hughJass.txt that contain a specified string. SearchHugh.java java SearchHugh syzygy spit result into a file named syzygy.finds