-4

I have a path i.e. "K:\user\abc.xml". I want to validate that path only that is valid or not, no need to create file at this location. I used file writer but it's create file. So please guide me what should I do?

Don't go with file please help me for path validation.

Kundan
  • 51
  • 4
  • 3
    Possible duplicate of [How do I check if a file exists in Java?](http://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-in-java) – Draken Mar 02 '17 at 14:45

2 Answers2

0

Create a File object:

File f = new File("K:\user\abc.xml");

and test if it exists:

if (f.exists()) {}
else {}
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • But I have an aplication which take path as in text box and pressing next button it will create file. But at some condition I don't want to create file, but user can enter anything in path like I have not 'k' drive so it should generate exception that path is not valid. – Kundan Mar 02 '17 at 14:41
  • I have an application which take path as in text box and after pressing next it will create file at that location, but at some condition I have not create file . But user can enter anything in this path field so I have not 'k' drive so it should generate exception that path is not valid. I want such type of output. Please suggest me – Kundan Mar 02 '17 at 14:43
  • The code does not create any file... Creating a `File` object does not creates a file. – Jean-Baptiste Yunès Mar 02 '17 at 14:44
  • Do u understand what I m saying? – Kundan Mar 02 '17 at 14:47
  • I dont want file.exist() . I want only path validation there is no such drive in my system so why should I check that file exist or not . Simple I want path is valid or not? – Kundan Mar 02 '17 at 14:51
  • Do you understand what you say? What does it means "path validation"? I know what it means if a path correspond to a file or not, but path validation is not clear. You mean syntactic validation? – Jean-Baptiste Yunès Mar 02 '17 at 16:09
  • Jean, valid path - D:\abc because D is your drive but not a valid path- X:\abc because X is not your drive. – Kundan Mar 02 '17 at 16:35
  • Then decompose the `File` with several calls to `parent()` and test exists on them. If you want, you can call `listRoots` and search for your drive. – Jean-Baptiste Yunès Mar 02 '17 at 19:30
  • Provide some generic solution, what happen if you have network path, I.e //10.1. 2.4/abc/abc.xml but that network is not accessible then it should also work like system drive path. – Kundan Mar 03 '17 at 00:49
0
File f = new File("c:/temp"); 

    if(f.exists()){
        System.out.println("yes");
    }else{
        System.out.println("no");
    }

This code will validate the given directory (or Path) is exists or not. Here are the possible results: if i have temp folder in c drive : yes if i don't have temp folder in c drive : no if you are writing to root directory like "C" path should "C:" (you should handle if user enter only "C" just append ":"

But, it purely dependent code for windows platform, you should think through platform independent.

Happy Codding.

Ravi
  • 63
  • 1
  • 8
  • It's ok if drive is your system drive, but what happen if drive is x, y, z I think this is not your system drive at this this it will show same output, But x is not a drive so it is invalid path, right? – Kundan Mar 02 '17 at 15:24
  • No x could be a drive, so such a path is valid... Now is the real problem not "does it reflect something existing or not?" – Jean-Baptiste Yunès Mar 02 '17 at 16:10
  • Put path which is not in your system like you have c drive D drive etc then you entered something else and then validate that it is valid path or not. – Kundan Mar 02 '17 at 16:45