0

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

Avinash Reddy
  • 2,204
  • 3
  • 25
  • 44
  • 1
    Why do you want to know this? Do you know network paths can be mapped to a drive letter as well? – CodeCaster Sep 09 '22 at 07:04
  • @CodeCaster I am supposed to read a file from a network path, so want to know this. I am not sure how to map network path to a drive letter. – Avinash Reddy Sep 09 '22 at 07:09
  • 1
    My point is that your code should not care whether a path points to a disk or a network. Why do you want to know where it points to? – CodeCaster Sep 09 '22 at 07:10
  • @CodeCaster There are multiple points here 1. If it is network path i need to check whether i have permission to that folder and file 2. Once my file is processed i need to a create a new folder in that path and make a copy of the processed file to the new folder created. – Avinash Reddy Sep 09 '22 at 08:23
  • 3. If it is absolute path i do a check in my code if the last character ends with "/" or "\" and if not i try to append "/" or "\". This check will not work for network path and it will try to append '/" or '\". if (folderPath.indexOf("\\") != -1) { folderPath = folderPath.endsWith("\\") ? folderPath : folderPath + "\\"; } else { folderPath = folderPath.endsWith("/") ? folderPath : folderPath + "/"; } – Avinash Reddy Sep 09 '22 at 08:31
  • 1
    https://stackoverflow.com/questions/412380/how-to-combine-paths-in-java, https://stackoverflow.com/questions/6231779/how-to-check-write-permissions-of-a-directory-in-java Again, you do not need to know (you literally should not care) whether a path points to a network. – CodeCaster Sep 09 '22 at 08:48
  • @CodeCaster one more question Also will the network path start with file : or will it start with // or \ file://LAPTOP-83N1BUOI/Documents //LAPTOP-83N1BUOI/Documents Can this be treated as network path file://LAPTOP-83N1BUOI/Documents ? – Avinash Reddy Sep 09 '22 at 10:50
  • Java's APIs do not really care about where you are reading data from as long as the URI is correct. – Prashant Sep 09 '22 at 12:21
  • 1
    A `File` is not a `URL`. Passing a URL to the `File` constructor just creates an invalid `File` object. That’s why all methods return `false`. If you want to access a URL, you have to use the `URL` class. But there isn’t a standard for accessing `file:` URLs on a different computer anyway. If you want to access a Windows share on a Windows computer, use Window’s network file syntax, i.e. `\\LAPTOP-83N1BUOI\Users\d_avi\OneDrive\Documents`. – Holger Sep 09 '22 at 15:33

0 Answers0