Files, Path

Java Collections
Level 1 , Lesson 3
Available

"Oh, Rishi! Hi!"

"Hi, Amigo! How's life?"

"Great. Bilaabo has just been telling me a bunch of interesting things about File, and how to work with it."

"As it happens, I think I have something to add on this topic."

"Really? Then I'm all ears."

"Okay, listen up. Java is constantly evolving. Its developers are constantly looking for new ways to do different things more efficiently. In Java 7, they added an alternative to the File class."

Files, Path - 1

"Alternative?"

"Yep. They took the File class as the basis, added some new stuff, renamed the methods, and then also split it into two. So now there are two new class: Path and FilesPath is actually the new analogue of the File class, and File is a utility class (analogous to the Arrays and Collections classes). All the static methods of the File class go there. Doing it this way is 'more correct' in terms of OOP."

"Well, if it's in terms of OOP, then okay. So what changed?"

"First, they decided not to reproduce the methods that returned String and File objects. In the Path class, all methods return a Path.

"Second, they moved a lot of static utility methods into the Files class."

"Third, it became more convenient to work with relative paths."

"Here is a list of the methods:"

Methods of the Path class Description
boolean isAbsolute() Returns true if the path is absolute.
Path getRoot() Returns the root of the current path, i.e. the topmost directory.
Path getFileName() Returns the file name from the current path.
Path getParent() Returns the directory from the current path.
boolean startsWith(Path other) Checks whether the current path begins with the passed path.
boolean endsWith(Path other) Checks whether the current path ends with the passed path.
Path normalize() Normalizes the current path. For example, converts «c:/dir/dir2/../a.txt» to «c:/dir/a.txt»
Path relativize(Path other) Calculates the relative path of two paths, i.e. «the difference between the paths»
Path resolve(String other) Resolves an absolute path using the current and relative paths.
URI toUri() Returns the URI if the current path/file.
Path toAbsolutePath() Converts the path to an absolute path if it is relative.
File toFile() Returns a File object that corresponds to the current Path object.

"And the current path—what's that?"

"This is the path that was passed to the constructor of the Path object whose methods are being called."

"OK. So what methods does the Files class have?"

"Are you in a hurry to get somewhere? I'll tell you right now. Here are the most important methods:"

Methods of the Files class Description
Path createFile(…) Creates a file on disk.
Path createDirectory(…) Creates a directory.
Path createDirectories(…) Creates a directory and all its subdirectories.
Path createTempFile(…) Creates a temporary file.
Path createTempDirectory(…) Creates a temporary directory.
void delete(Path path) Deletes a file.
Path copy(Path source, Path target,…) Copies a file.
Path move(Path source, Path target,…) Moves a file.
boolean isSameFile(Path, Path) Compares two files.
boolean isDirectory(Path) Is the Path a directory?
boolean isRegularFile(Path) Is the Path a file?
long size(Path) Returns the file size.
boolean exists(Path) Does an object with the same name exist?
boolean notExists(Path) Does an object with the same name not exist?
long copy(InputStream, OutputStream) Copies bytes from the InputStream to the OutputStream.
long copy(Path, OutputStream) Copies all bytes from the Path to the OutputStream.
long copy(InputStream, Path) Copies all bytes from the InputStream to the Path.
byte[] read(InputStream, int initialSize) Reads an array of bytes from the InputStream.
byte[] readAllBytes(Path path) Reads all bytes from the InputStream.
List<String> readAllLines(Path path,..) Reads the text file and returns a list of strings.
Path write(Path path, byte[] bytes,…) Writes an array of bytes to a file.

"How interesting! So many cool functions, and all in the same place."

"Well look. Suppose you want to download a file from the Internet, and then send it to someone. To do this, it's very convenient to create a temporary file on disk and save the read data to it."

"Is it difficult to download a file from the Internet?"

"It's very simple. Look at this example:"

Code
URL url = new URL("https://www.google.com.ua/images/srpr/logo11w.png");
InputStream inputStream = url.openStream();

Path tempFile = Files.createTempFile("temp-",".tmp");
Files.copy(inputStream, tempFile);

"And that's it?"

"Yes, what did you expect to see? There are only 4 lines."

"Line 1. Creates a URL object, to which the URL of the image file is passed.

"Line 2. A stream for reading a file (InputStream) is opened on the url object.

"Line 3. The createTempFile method is used to create a temporary file.

"Line 4. The Files.copy method copies data from inputStream to tempFile. That's it."

"Ingenious!"

"Great. I'm glad you like it. I think you can figure out the rest of the methods yourself. And I'll ask Diego to give you several tasks about them."

"By the way, here's a good link on this material"

Comments (7)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
HugoAl Level 23, France
30 September 2022
For first time i need correction for all the tasks of the previous tasks. It would be better to talk about the differents interface of this topic.https://codegym.cc/groups/posts/219-files-path
Matthias Wanner Level 23, Detmold, Germany
14 February 2022
lol. I spent hours to solve the task in the previous lesson. this would have helped a lot. I am pissed.
Jurij Thmsn Level 29, Flensburg, Germany
11 May 2021
read this before doing the exercises and save a lot of nerves: https://codegym.cc/groups/posts/219-files-path
Bart De Lepeleer Level 30, Belgium
21 December 2020
I used this code as boilerplate for the next exercise.

Files.copy(inputStream, tempFile) 
throws an exception unless you use :

Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
BlueJavaBanana Level 37
23 December 2020
Nice tip, thanks!
季军 Level 38, Shanghai, China
10 August 2019
void delete(Path path) Creates a temporary directory. I think the description here is wrong
hurzel purzel Level 22, Stuttgart, Germany
17 July 2019
Is it just me or does this lesson omit how to actually open an existing file (using Path and Files)?