How to differentiate between a network path and absolute path and read a file from network path in java

Viewed 60

I have a field where a user can specify an absolute path or network path.enter image description here

In the Excel Folder Path user can give an absolute path or network path.

Absolute Path -> C:\ExcelExtractor

Network Path -> file://LAPTOP-83N1BUOI/Users/d_avi/OneDrive/Documents

Also will the network path start with file : or will it start with // or \

In my java code i get the value of Excel Folder Path

Here is my sample java code.

public class Testing {

    public static void main(String[] args) {
        
        File file = new File("file://LAPTOP-83N1BUOI/Users/d_avi/OneDrive/Documents");

        String fileName = file.getName();

        System.out.println("Is file " + file.isFile()); 
        System.out.println("Directory " + file.isDirectory());
        System.out.println("Absolute " + file.isAbsolute());
        System.out.println("fileName is " + fileName);
        System.out.println("Can Read " + file.canRead());
        System.out.println("Can Write " + file.canWrite());

        File file1 = new File("D:\\File Testing");
        System.out.println("Directory" + file1.isDirectory());
        System.out.println("Absolute" + file1.isAbsolute());

    }

Here is the output of the code

Is file false -> this condition is clear, because the path given is till a folder
Directory false
Absolute false
fileName is Documents
Can Read false
Can Write false

Directory true
Absolute true

I have given all the sharing and permissions to the Documents folder, but still isDirectory() returns false, canRead() returns false, canWrite () returns false.

I agree with the condition Is file returning false because the path is given till a directory

Need help and suggestions on the following

- How to decide whether it is network path

- How to read a file from network path

- How to create a new folder in network path and copy a file to that folder

0 Answers
Related