Files - a file class that contains static methods that operates on files and directories.
Checking a file - you need to verify if the file does exist and check what you can do with it. Take note that before using the program, you need to create a file or a text document first to test or else the result would be your files does exist all the time.
=================
Class PracticingFile{
public static void main(String[] args) throws IOException{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your file below:");
String direction = scanner.nextLine();
Path objectPath1 = Path.of(direction);
Path path2 = objectPath1.toAbsolutePath();
if(Files.exist(path2)){
System.out.println("Your file: " + path2.getParent());
if(Files.isReadable(path2)){
System.out.println("file: Readable");
}else {
System.out.println("file: Not readable");
}
}else {
System.out.println("Your file does not exist.");
}
scanner.close();
}
}
=================
Result:
Enter your file below:
YourTempFile.txt
Your file: c:\Folder1\subfolder\anotherSubfolder\YourTempFile.txt
file: Readable
Note: Other static functions of Files class to use for checking are isWritable(), isHidden(), isSymbolicLink(), isExecutable() and so on.
YOU ARE READING
Java Programming
Randomhttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...