Deleting, Copying And Moving Files

6 0 0
                                    

Files - is used to manipulate things related to files like deleting, copying, checking, moving, reading, writing, creating and so on.

let:

Path file = Path.of("YourTempFile.txt");

Path path = file.toAbsolutePath();

Deleting a file: (IOException, NoSuchFileException, DirectoryNotEmptyException)

          1. Files.delete(path)

          2. Files.deleteIfExist(path)

Copying a file: (IOExceoption, DirectoryNotEmptyException)

          - only the directory is copied and not what is inside the file.

          1. Files.copy(sourcePath, targetPath, CopyOption...)

          CopyOption - is a vararg. You can put more than one of the StandardCopyOption constant.

                    1.1. REPLACE_EXISTING - perform copy when the target does exist.

                    1.2 COPY_ATTRIBUTES - copy the attributes that existed with the source file.

                    1.3 NOFOLLOW_LINKS - the symbolic link is copied but not followed. Only the link and not the target of the link. This one cannot be found in Intellij IDE.

           2. Files.copy(InputStream, targetPath, CopyOption...)

           3. Files.copy(OutputStream, targetPath)

Moving a file or a directory: ()

          - You can move a file or a directory especially if empty. If not empty, it is allowed as long as the contents is not move. 

          1. Files.move(sourcePath, targetPath, CopyOption...)

          CopyOption - is a vararg. You can put more than one of the StandardCopyOption constant.

                    1.1. REPLACE_EXISTING - performs move when the target does exist.

                    1.2 ATOMIC_MOVE - performs the move as an atomic operation

=====================

import java.io.IOException;
import java.nio.file.*;
import java.util.Scanner;class StreamClassesPractice {

public static void main(String[] args) throws IOException {

Scanner scanner1 = new Scanner(System.in);
Scanner scanner2 = new Scanner(System.in);

System.out.println("Enter a file name in here (please do include the subtext like .txt): ");
String yourFile = scanner1.nextLine();
Path path = Path.of(yourFile);
Path path2 = path.toAbsolutePath();

System.out.println("""
File functions
1. Checking
2. Deleting
3. Copying source to target
4. Moving source to target
""");


System.out.print("Enter a number to perform: ");
int funcNumber = scanner1.nextInt();


switch (funcNumber) {
case 1 -> checkingFile(path2);
case 2 -> deletingFile(path2);
case 3 -> {
System.out.println("Enter a target file:");
String targetFile = scanner2.nextLine();
Path tempPath = Path.of(targetFile);
Path targetPath = tempPath.toAbsolutePath();
copyingFile(path2, targetPath);
}
case 4 -> {
System.out.println("Enter a target file:");
String targetFile = scanner2.nextLine();
Path tempPath = Path.of(targetFile);
Path targetPath = tempPath.toAbsolutePath();
movingFile(path2, targetPath);
}
default -> System.out.println("Choose only the number listed above!");
}


scanner1.close();
scanner2.close();
}
private static void checkingFile(Path thePath) throws IOException{
if(Files.exists(thePath)) {
System.out.println("Your file: " + thePath.getParent());


if(Files.isReadable(thePath)){
System.out.println("file: readable");
}else {
System.out.println("file: Not readable");
}


if(Files.isWritable(thePath)){
System.out.println("file: writeable");
}else {
System.out.println("file: Not writable");
}


if(Files.isExecutable(thePath)){
System.out.println("file: executable");
}else{
System.out.println("file: Not executable");
}


if(Files.isSymbolicLink(thePath)){
System.out.println("file: a symbolic link");
}else{
System.out.println("file: Not a symbolic link");
}


if(Files.isHidden(thePath)){
System.out.println("file: hidden");
}else{
System.out.println("file: Not hidden");
}


}else{
System.out.println("Your file does not exist");
}
}
private static void deletingFile(Path thePath) throws IOException{

Files.deleteIfExists(thePath);
System.out.println("Just deleted: " + thePath);

}

private static void copyingFile(Path sourcePath, Path targetPath) throws IOException{

if(Files.exists(targetPath) || Files.isSymbolicLink(targetPath)) {

Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Replace existing: " + sourcePath + " to: " + targetPath);

} else {

Files.copy(sourcePath, targetPath, StandardCopyOption.COPY_ATTRIBUTES);
System.out.println("Copied attributes: " + sourcePath + " to: " + targetPath);
}
}

private static void movingFile(Path sourcePath, Path targetPath) throws IOException{

if(Files.exists(targetPath) || Files.isSymbolicLink(targetPath)){

Files.move(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Replace existing: " + sourcePath + " to: " + targetPath);

}else {

Files.move(sourcePath, targetPath, StandardCopyOption.ATOMIC_MOVE);
System.out.println("Atomic move: " + sourcePath + " to: " + targetPath);

}
}

}

=====================

Result:

Enter a file name in here (please do include the subtext like .txt):

TestingPath.txt

File functions

1. Checking

2. Deleting

3. Copying source to target

4. Moving source to target

Enter a number to perform: 4

Enter a target file:

TargetPath.txt

Replace existing: C:\Folder1\Sub Folder1\Sub Folder2\Sub Folder 3\TestingPath.txt to: C:\Folder1\Sub Folder1\Sub Folder2\Sub Folder 3\TargetPath.txt


Note: These two files was already created. I just copied the program i did in the IDE.

Java ProgrammingWhere stories live. Discover now