0% found this document useful (0 votes)
3 views4 pages

Java tutorial

The document outlines methods for creating, moving, copying, and deleting files and directories in Java. It details specific methods such as createDirectory(), copy(), move(), and delete(), along with their behaviors and exceptions. Additionally, it discusses options like REPLACE_EXISTING and ATOMIC_MOVE for handling existing files during these operations.

Uploaded by

Autist Infantilo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Java tutorial

The document outlines methods for creating, moving, copying, and deleting files and directories in Java. It details specific methods such as createDirectory(), copy(), move(), and delete(), along with their behaviors and exceptions. Additionally, it discusses options like REPLACE_EXISTING and ATOMIC_MOVE for handling existing files during these operations.

Uploaded by

Autist Infantilo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Creating, Moving, and Deleting

Files and Directories


Making Directories

public static Path createDirectory(Path dir,


FileAttribute<?>… attrs) throws IOException

public static Path createDirectories(Path dir,


FileAttribute<?>… attrs) throws IOException

The createDirectory() method will create a directory and throw an exception if it


already exists or if the paths leading up to the directory do not exist.
The createDirectories() method creates the target directory along with any nonexistent
parent directories leading up to the path. If all of the directories already
exist, createDirectories() will simply complete without doing anything.

Copying Files

public static Path copy(Path source, Path target,


CopyOption… options) throws IOException

When directories are copied, the copy is shallow. A shallow copy means that the
files and subdirectories within the directory are not copied.

Copying and Replacing Files


By default, if the target already exists, the copy() method will throw an exception.
You can change this behavior by providing the StandardCopyOption enum
value REPLACE_EXISTING

Files.copy(Path.of("book.txt"), Path.of("movie.txt"),
StandardCopyOption.REPLACE_EXISTING);

Creating, Moving, and Deleting Files and Directories 1


For the exam, you need to know that without the REPLACE_EXISTING option, this
method will throw an exception if the file already exists.

Copying Files with I/O Streams


The Files class includes two copy() methods that operate with I/O streams.

public static long copy(InputStream in, Path target,


CopyOption… options) throws IOException

public static long copy(Path source, OutputStream out)


throws IOException

The first method reads the contents of an I/O stream and writes the output to a
file. The second method reads the contents of a file and writes the output to an I/O
stream.

try (var is = new FileInputStream("source-data.txt")) {


// Write I/O stream data to a file
Files.copy(is, Path.of("/mammals/wolf.txt"));
}

Files.copy(Path.of("/fish/clown.xsl"), System.out);

Copying Files into a Directory

var file = Path.of("food.txt");


var directory = Path.of("/enclosure/food.txt");
Files.copy(file, directory);

incorrect way, throws exception:

Creating, Moving, and Deleting Files and Directories 2


var file = Path.of("food.txt");
var directory = Path.of("/enclosure");
Files.copy(file, directory);

Moving or Renaming Paths with move()

public static Path move(Path source, Path target,


CopyOption… options) throws IOException

Files.move(Path.of("C:\\zoo"), Path.of("C:\\zoo-new"));

Files.move(Path.of("C:\\user\\addresses.txt"),
Path.of("C:\\zoo-new\\addresses2.txt"));

The first example renames the zoo directory to a zoo-new directory, keeping all of
the original contents from the source directory. The second example moves
the addresses.txt file from the directory user to the directory zoo-new and renames
it addresses2.txt .
Like copy() , move() requires REPLACE_EXISTING to overwrite the target if it exists;
otherwise, it will throw an exception

Another enum value that you need to know for the exam when working with
the move() method is the StandardCopyOption value ATOMIC_MOVE .

Files.move(Path.of("mouse.txt"), Path.of("gerbil.txt"),
StandardCopyOption.ATOMIC_MOVE);

An atomic move is one in which a file is moved within the file system as a single
indivisible operation. Put another way, any process monitoring the file system
never sees an incomplete or partially written file. If the file system does not
support this feature, an AtomicMoveNotSupportedException will be thrown.

Creating, Moving, and Deleting Files and Directories 3


Deleting a File with delete() and deleteIfExists()

public static void delete(Path path) throws IOException

public static boolean deleteIfExists(Path path) throws IOException

To delete a directory, it must be empty. Both of these methods throw an exception


if operated on a nonempty directory
The delete() method throws an exception if the path does not exist, while
the deleteIfExists() method returns true if the delete was successful
or false otherwise.

Files.delete(Path.of("/vulture/feathers.txt"));
Files.deleteIfExists(Path.of("/pigeon"));

Comparing Files with isSameFile() and mismatch()


isSameFile() method takes two Path objects as input, resolves all path symbols,
and follows symbolic links. The method can also be used to determine whether
two Path objects refer to the same directory.
If the two path objects are equal in terms of equals() , the method will just
return true without checking whether the file exists.

Sometimes you want to compare the contents of the file rather than whether it is
physically the same file
The mismatch() method takes two Path objects as input. The method returns -1 if
the files are the same; otherwise, it returns the index of the first position in the file
that differs.

Creating, Moving, and Deleting Files and Directories 4

You might also like